|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + "github.com/Bonial-International-GmbH/terraform-provider-spinnaker/spinnaker/api/errors" |
| 7 | + "github.com/antihax/optional" |
| 8 | + "github.com/mitchellh/mapstructure" |
| 9 | + gate "github.com/spinnaker/spin/cmd/gateclient" |
| 10 | + gateapi "github.com/spinnaker/spin/gateapi" |
| 11 | +) |
| 12 | + |
| 13 | +// CreatePipelineTemplateV2 creates a pipeline template. |
| 14 | +func CreatePipelineTemplateV2(client *gate.GatewayClient, template *PipelineTemplateV2) error { |
| 15 | + _, resp, err := retry(func() (map[string]interface{}, *http.Response, error) { |
| 16 | + return client.V2PipelineTemplatesControllerApi.CreateUsingPOST1(client.Context, template, nil) |
| 17 | + }) |
| 18 | + if err != nil || (resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated) { |
| 19 | + return errors.NewResponseError(resp, err) |
| 20 | + } |
| 21 | + |
| 22 | + return nil |
| 23 | +} |
| 24 | + |
| 25 | +// GetPipelineTemplateV2 fetches the pipeline template with templateID. |
| 26 | +func GetPipelineTemplateV2(client *gate.GatewayClient, templateID string) (*PipelineTemplateV2, error) { |
| 27 | + payload, resp, err := retry(func() (map[string]interface{}, *http.Response, error) { |
| 28 | + return client.V2PipelineTemplatesControllerApi.GetUsingGET2(client.Context, templateID, nil) |
| 29 | + }) |
| 30 | + if err != nil || resp.StatusCode != http.StatusOK { |
| 31 | + return nil, errors.NewResponseError(resp, err) |
| 32 | + } |
| 33 | + |
| 34 | + var template PipelineTemplateV2 |
| 35 | + |
| 36 | + if err := mapstructure.Decode(payload, &template); err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + |
| 40 | + return &template, nil |
| 41 | +} |
| 42 | + |
| 43 | +// DeletePipelineTemplateV2 deletes the pipeline template with templateID. |
| 44 | +// Either digest or tag can be set on a delete request, but not both. |
| 45 | +func DeletePipelineTemplateV2(client *gate.GatewayClient, templateID, tag, digest string) error { |
| 46 | + opts := &gateapi.V2PipelineTemplatesControllerApiDeleteUsingDELETE1Opts{} |
| 47 | + if digest != "" { |
| 48 | + opts.Digest = optional.NewString(digest) |
| 49 | + } else if tag != "" { |
| 50 | + opts.Tag = optional.NewString(tag) |
| 51 | + } |
| 52 | + |
| 53 | + _, resp, err := retry(func() (map[string]interface{}, *http.Response, error) { |
| 54 | + return client.V2PipelineTemplatesControllerApi.DeleteUsingDELETE1(client.Context, templateID, opts) |
| 55 | + }) |
| 56 | + if err != nil || (resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent) { |
| 57 | + return errors.NewResponseError(resp, err) |
| 58 | + } |
| 59 | + |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +// UpdatePipelineTemplateV2 updates the pipeline template with templateID with |
| 64 | +// the data in template. |
| 65 | +func UpdatePipelineTemplateV2(client *gate.GatewayClient, template *PipelineTemplateV2) error { |
| 66 | + _, resp, err := retry(func() (map[string]interface{}, *http.Response, error) { |
| 67 | + return client.V2PipelineTemplatesControllerApi.UpdateUsingPOST1(client.Context, template.ID, template, nil) |
| 68 | + }) |
| 69 | + if err != nil || (resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated) { |
| 70 | + return errors.NewResponseError(resp, err) |
| 71 | + } |
| 72 | + |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +// ListPipelineTemplateV2Versions lists versions of all available pipeline |
| 77 | +// templates. The resulting map is keyed by template ID. |
| 78 | +func ListPipelineTemplateV2Versions(client *gate.GatewayClient) (map[string][]*PipelineTemplateV2Version, error) { |
| 79 | + var payload interface{} |
| 80 | + |
| 81 | + _, resp, err := retry(func() (map[string]interface{}, *http.Response, error) { |
| 82 | + v, resp, err := client.V2PipelineTemplatesControllerApi.ListVersionsUsingGET(client.Context, nil) |
| 83 | + payload = v |
| 84 | + return nil, resp, err |
| 85 | + }) |
| 86 | + if err != nil || resp.StatusCode != http.StatusOK { |
| 87 | + return nil, errors.NewResponseError(resp, err) |
| 88 | + } |
| 89 | + |
| 90 | + var versionMap map[string][]*PipelineTemplateV2Version |
| 91 | + |
| 92 | + if err = mapstructure.Decode(payload, &versionMap); err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + |
| 96 | + return versionMap, nil |
| 97 | +} |
0 commit comments