Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for providing a custom request body for liquid requests #248

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading