Skip to content

Commit

Permalink
Merge pull request #248 from sapcc/custom-request-body
Browse files Browse the repository at this point in the history
Add option for providing a custom request body for liquid requests
  • Loading branch information
majewsky authored Dec 17, 2024
2 parents 13e5d1f + 4bf42c3 commit 71046ab
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
2 changes: 2 additions & 0 deletions internal/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,13 @@ func (o rateOutputFmtFlags) validate() (*core.OutputOpts, error) {
type liquidOperationFlags struct {
endpoint string
compare bool
body string
}

func (l *liquidOperationFlags) AddToCmd(cmd *cobra.Command) {
cmd.Flags().StringVarP(&l.endpoint, "endpoint", "e", "", "query a liquid running locally")
cmd.Flags().BoolVarP(&l.compare, "compare", "c", false, "query both the liquid in the cluster and the liquid running locally. Renders the diff of both responses. Requires --endpoint to be set.")
cmd.Flags().StringVarP(&l.body, "body", "b", "", "use a custom request body generated from provided json structure")
}

// liquidQuotaOperationFlags
Expand Down
66 changes: 46 additions & 20 deletions internal/cmd/liquid.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func (c *liquidServiceInfoCmd) Run(cmd *cobra.Command, args []string) error {
if compare && (endpoint == "" || serviceType == "") {
return errors.New("argument $SERVICE_TYPE and flag --endpoint are both required for comparison mode")
}
body := c.liquidOperationFlags.body
if body != "" {
return errors.New("custom request body is not needed when retrieving service info")
}

provider, err := authenticate(cmd.Context())
if err != nil {
Expand Down Expand Up @@ -197,18 +201,29 @@ func (c *liquidReportCapacityCmd) Run(cmd *cobra.Command, args []string) error {
if compare && (endpoint == "" || serviceType == "") {
return errors.New("argument $SERVICE_TYPE and flag --endpoint are both required for comparison mode")
}

var res gophercloud.Result
resp, err := limesAdminClient.Get(cmd.Context(), limesAdminClient.ServiceURL("liquid/service-capacity-request?service_type=liquid-"+serviceType), &res.Body, nil) //nolint:bodyclose
_, res.Header, res.Err = gophercloud.ParseResponse(resp, err)
if err != nil {
return util.WrapError(err, "could not fetch service capacity request body from limes")
}
body := c.liquidOperationFlags.body

var serviceCapacityRequest *liquid.ServiceCapacityRequest
err = res.ExtractInto(&serviceCapacityRequest)
if err != nil {
return util.WrapError(err, "could not parse service capacity request body response from limes")
if body == "" {
var res gophercloud.Result
resp, err := limesAdminClient.Get(cmd.Context(), limesAdminClient.ServiceURL("liquid/service-capacity-request?service_type=liquid-"+serviceType), &res.Body, nil) //nolint:bodyclose
_, res.Header, res.Err = gophercloud.ParseResponse(resp, err)
if err != nil {
return util.WrapError(err, "could not fetch service capacity request body from limes")
}

err = res.ExtractInto(&serviceCapacityRequest)
if err != nil {
return util.WrapError(err, "could not parse service capacity request body response from limes")
}
} else {
decoder := json.NewDecoder(strings.NewReader(body))
decoder.DisallowUnknownFields()

err := decoder.Decode(&serviceCapacityRequest)
if err != nil {
return util.WrapError(err, "could not parse custom body parameters into service capacity request")
}
}

provider, err := authenticate(cmd.Context())
Expand Down Expand Up @@ -301,18 +316,29 @@ func (c *liquidReportUsageCmd) Run(cmd *cobra.Command, args []string) error {
if compare && (endpoint == "" || serviceType == "") {
return errors.New("argument $SERVICE_TYPE and flag --endpoint are both required for comparison mode")
}

var r gophercloud.Result
resp, err := limesAdminClient.Get(cmd.Context(), limesAdminClient.ServiceURL(fmt.Sprintf("liquid/service-usage-request?service_type=%s&project_id=%s", serviceType, projectID)), &r.Body, nil) //nolint:bodyclose
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
if err != nil {
return util.WrapError(err, "could not fetch service usage request body from limes")
}
body := c.liquidOperationFlags.body

var serviceUsageRequest *liquid.ServiceUsageRequest
err = r.ExtractInto(&serviceUsageRequest)
if err != nil {
return util.WrapError(err, "could not parse service usage request body response from limes")
if body == "" {
var r gophercloud.Result
resp, err := limesAdminClient.Get(cmd.Context(), limesAdminClient.ServiceURL(fmt.Sprintf("liquid/service-usage-request?service_type=%s&project_id=%s", serviceType, projectID)), &r.Body, nil) //nolint:bodyclose
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
if err != nil {
return util.WrapError(err, "could not fetch service usage request body from limes")
}

err = r.ExtractInto(&serviceUsageRequest)
if err != nil {
return util.WrapError(err, "could not parse service usage request body response from limes")
}
} else {
decoder := json.NewDecoder(strings.NewReader(body))
decoder.DisallowUnknownFields()

err := decoder.Decode(&serviceUsageRequest)
if err != nil {
return util.WrapError(err, "could not parse custom body parameters into service usage request")
}
}

provider, err := authenticate(cmd.Context())
Expand Down

0 comments on commit 71046ab

Please sign in to comment.