Skip to content

Commit

Permalink
api options
Browse files Browse the repository at this point in the history
  • Loading branch information
mildwonkey committed Oct 25, 2024
1 parent ebdfc7c commit 7d36924
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
5 changes: 2 additions & 3 deletions src/pkg/domains/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"net/http"
"net/url"
"time"

"github.com/defenseunicorns/lula/src/types"
)
Expand All @@ -34,8 +33,8 @@ func (a ApiDomain) makeRequests(_ context.Context) (types.DomainResources, error
}

defaultClient := &http.Client{Transport: transport}
if defaultOpts.Timeout != 0 {
defaultClient.Timeout = time.Duration(defaultOpts.Timeout) * time.Second
if defaultOpts.timeout != nil {
defaultClient.Timeout = *defaultOpts.timeout
}

for _, request := range a.Spec.Requests {
Expand Down
36 changes: 30 additions & 6 deletions src/pkg/domains/api/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,51 @@ package api
import (
"errors"
"fmt"
"time"
)

func validateSpec(spec *ApiSpec) error {
// validateAndMutateSpec validates the spec values and applies any defaults or
// other mutations necessary. The original values are not modified.
// validateAndMutateSpec will validate the entire object and may return multiple
// errors.
func validateAndMutateSpec(spec *ApiSpec) (errs error) {
if spec == nil {
return errors.New("spec is required")
}
if len(spec.Requests) == 0 {
return errors.New("some requests must be specified")
errs = errors.Join(errors.New("some requests must be specified"))
}

if spec.Options != nil {
if spec.Options.Timeout != "" {
duration, err := time.ParseDuration(spec.Options.Timeout)
if err != nil {
errs = errors.Join(fmt.Errorf("invalid wait timeout string: %s", spec.Options.Timeout))
}
spec.Options.timeout = &duration
}
} else {
// add an Options struct with a sane default timeout
spec.Options = &ApiOpts{}
}
if spec.Options.timeout == nil {
d := 30 * time.Second
spec.Options.timeout = &d
}

for _, request := range spec.Requests {
if request.Name == "" {
return errors.New("request name cannot be empty")
errs = errors.Join(errors.New("request name cannot be empty"))
}
if request.URL == "" {
return errors.New("request url cannot be empty")
errs = errors.Join(errors.New("request url cannot be empty"))
}
if request.Method != "" {
if request.Method != "Get" && request.Method != "Head" && request.Method != "Post" && request.Method != "PostForm" {
return fmt.Errorf("unsupported method: %s", request.Method)
errs = errors.Join(fmt.Errorf("unsupported method: %s", request.Method))
}
}
}
return nil

return errs
}
6 changes: 5 additions & 1 deletion src/pkg/domains/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"context"
"time"

"github.com/defenseunicorns/lula/src/types"
)
Expand All @@ -14,7 +15,7 @@ type ApiDomain struct {

func CreateApiDomain(spec *ApiSpec) (types.Domain, error) {
// Check validity of spec
err := validateSpec(spec)
err := validateAndMutateSpec(spec)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -57,4 +58,7 @@ type ApiOpts struct {
Timeout string `json:"timeout,omitempty" yaml:"timeout,omitempty"`
Proxy string `json:"proxy,omitempty" yaml:"proxy,omitempty"`
Headers []string `json:"headers,omitempty" yaml:"headers,omitempty"`

// internally-managed options
timeout *time.Duration
}

0 comments on commit 7d36924

Please sign in to comment.