Repository: hashicorp/vault
File: api/sys_plugins.go (Go)

Code before:
	defer cancelFunc()

	path := catalogPathByType(i.Type, i.Name)
	req := c.c.NewRequest(http.MethodPut, path)

	if err := req.SetJSONBody(i); err != nil {
		return nil, err
	}

	resp, err := c.c.rawRequestWithContext(ctx, req)
	if err == nil {
		defer resp.Body.Close()
	}

	var registerResp RegisterPluginResponse
	err = resp.DecodeJSON(&registerResp)
	if err != nil {
		return nil, err
	}

	// Filter out the `Endpoint replaced the value of these parameters with the values captured from the endpoint's path: [type]`
	// warning because it is expected behavior from this function, as we set the type parameter in both the path and request body,
	// and the warning informs us the path parameter takes precedence. However, this warning is not relevant for an end user so we
	// omit it before returning to any client.
	// TODO: This can likely be removed once https://hashicorp.atlassian.net/browse/VAULT-36722 is addressed.
	filteredWarnings := make([]string, 0, len(registerResp.Warnings))
	for _, warning := range registerResp.Warnings {
		if !strings.Contains(warning, "Endpoint replaced the value of these parameters with the values captured from the endpoint's path") {
			filteredWarnings = append(filteredWarnings, warning)
		}
	}
	registerResp.Warnings = filteredWarnings

	return &registerResp, err
}

// DeregisterPluginInput is used as input to the DeregisterPlugin function.
type DeregisterPluginInput struct {
	// Name is the name of the plugin. Required.
	Name string `json:"-"`

	// Type of the plugin. Required.
	Type PluginType `json:"type"`

	// Version of the plugin. Optional.
	Version string `json:"version,omitempty"`
}

// DeregisterPlugin wraps DeregisterPluginWithContext using context.Background.
func (c *Sys) DeregisterPlugin(i *DeregisterPluginInput) error {
	return c.DeregisterPluginWithContext(context.Background(), i)

Review comment (suggestion):
Slight optimization suggestion. We could make the filtering conditional on there being warnings. That saves us one new slice.

Code after:
}

// RegisterPluginWithContext registers the plugin with the given information.
// Deprecated: Use RegisterPluginWithContextDetailed instead.
func (c *Sys) RegisterPluginWithContext(ctx context.Context, i *RegisterPluginInput) error {
	ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
	defer cancelFunc()

	path := catalogPathByType(i.Type, i.Name)
	req := c.c.NewRequest(http.MethodPut, path)

	if err := req.SetJSONBody(i); err != nil {
		return err
	}

	resp, err := c.c.rawRequestWithContext(ctx, req)
	if err == nil {
		defer resp.Body.Close()
	}
	return err
}

// RegisterPluginDetailed wraps RegisterPluginWtihContextDetailed using context.Background.
func (c *Sys) RegisterPluginDetailed(i *RegisterPluginInput) (*RegisterPluginResponse, error) {
	return c.RegisterPluginWithContextDetailed(context.Background(), i)
}

// RegisterPluginWithContextDetailed registers the plugin with the given information.
func (c *Sys) RegisterPluginWithContextDetailed(ctx context.Context, i *RegisterPluginInput) (*RegisterPluginResponse, error) {
	ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
	defer cancelFunc()

	path := catalogPathByType(i.Type, i.Name)
	req := c.c.NewRequest(http.MethodPut, path)

	if err := req.SetJSONBody(i); err != nil {
		return nil, err
	}

	resp, err := c.c.rawRequestWithContext(ctx, req)
	if resp != nil {
		defer resp.Body.Close()
	}
	if err != nil {
		return nil, err
	}

	var registerResp RegisterPluginResponse
	if resp != nil && resp.StatusCode != http.StatusNoContent {
		if err := resp.DecodeJSON(&registerResp); err != nil {
			return nil, err