|
| 1 | +package buildkite |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | +) |
| 7 | + |
| 8 | +// ClusterSecretsService handles communication with cluster secret related |
| 9 | +// methods of the Buildkite API. |
| 10 | +// |
| 11 | +// Buildkite API docs: https://buildkite.com/docs/apis/rest-api/clusters#cluster-secrets |
| 12 | +type ClusterSecretsService struct { |
| 13 | + client *Client |
| 14 | +} |
| 15 | + |
| 16 | +type ClusterSecret struct { |
| 17 | + ID string `json:"id,omitempty"` |
| 18 | + GraphQLID string `json:"graphql_id,omitempty"` |
| 19 | + Key string `json:"key,omitempty"` |
| 20 | + Description string `json:"description,omitempty"` |
| 21 | + Policy string `json:"policy,omitempty"` |
| 22 | + URL string `json:"url,omitempty"` |
| 23 | + ClusterURL string `json:"cluster_url,omitempty"` |
| 24 | + CreatedAt *Timestamp `json:"created_at,omitempty"` |
| 25 | + CreatedBy ClusterCreator `json:"created_by"` |
| 26 | + UpdatedAt *Timestamp `json:"updated_at,omitempty"` |
| 27 | + UpdatedBy ClusterCreator `json:"updated_by"` |
| 28 | + LastReadAt *Timestamp `json:"last_read_at,omitempty"` |
| 29 | + Organization string `json:"organization,omitempty"` |
| 30 | +} |
| 31 | + |
| 32 | +type ClusterSecretCreate struct { |
| 33 | + Key string `json:"key"` |
| 34 | + Value string `json:"value,omitempty"` |
| 35 | + Description string `json:"description,omitempty"` |
| 36 | + Policy string `json:"policy,omitempty"` |
| 37 | +} |
| 38 | + |
| 39 | +type ClusterSecretUpdate struct { |
| 40 | + Description string `json:"description,omitempty"` |
| 41 | + Policy string `json:"policy,omitempty"` |
| 42 | +} |
| 43 | + |
| 44 | +type ClusterSecretValueUpdate struct { |
| 45 | + Value string `json:"value"` |
| 46 | +} |
| 47 | + |
| 48 | +type ClusterSecretsListOptions struct { |
| 49 | + ListOptions |
| 50 | +} |
| 51 | + |
| 52 | +func (css *ClusterSecretsService) List(ctx context.Context, org, clusterID string, opt *ClusterSecretsListOptions) ([]ClusterSecret, *Response, error) { |
| 53 | + u := fmt.Sprintf("v2/organizations/%s/clusters/%s/secrets", org, clusterID) |
| 54 | + u, err := addOptions(u, opt) |
| 55 | + if err != nil { |
| 56 | + return nil, nil, err |
| 57 | + } |
| 58 | + |
| 59 | + req, err := css.client.NewRequest(ctx, "GET", u, nil) |
| 60 | + if err != nil { |
| 61 | + return nil, nil, err |
| 62 | + } |
| 63 | + |
| 64 | + var secrets []ClusterSecret |
| 65 | + resp, err := css.client.Do(req, &secrets) |
| 66 | + if err != nil { |
| 67 | + return nil, resp, err |
| 68 | + } |
| 69 | + |
| 70 | + return secrets, resp, err |
| 71 | +} |
| 72 | + |
| 73 | +func (css *ClusterSecretsService) Get(ctx context.Context, org, clusterID, secretID string) (ClusterSecret, *Response, error) { |
| 74 | + u := fmt.Sprintf("v2/organizations/%s/clusters/%s/secrets/%s", org, clusterID, secretID) |
| 75 | + req, err := css.client.NewRequest(ctx, "GET", u, nil) |
| 76 | + if err != nil { |
| 77 | + return ClusterSecret{}, nil, err |
| 78 | + } |
| 79 | + |
| 80 | + var secret ClusterSecret |
| 81 | + resp, err := css.client.Do(req, &secret) |
| 82 | + if err != nil { |
| 83 | + return ClusterSecret{}, resp, err |
| 84 | + } |
| 85 | + |
| 86 | + return secret, resp, err |
| 87 | +} |
| 88 | + |
| 89 | +func (css *ClusterSecretsService) Create(ctx context.Context, org, clusterID string, input ClusterSecretCreate) (ClusterSecret, *Response, error) { |
| 90 | + u := fmt.Sprintf("v2/organizations/%s/clusters/%s/secrets", org, clusterID) |
| 91 | + req, err := css.client.NewRequest(ctx, "POST", u, input) |
| 92 | + if err != nil { |
| 93 | + return ClusterSecret{}, nil, err |
| 94 | + } |
| 95 | + |
| 96 | + var secret ClusterSecret |
| 97 | + resp, err := css.client.Do(req, &secret) |
| 98 | + if err != nil { |
| 99 | + return ClusterSecret{}, resp, err |
| 100 | + } |
| 101 | + |
| 102 | + return secret, resp, err |
| 103 | +} |
| 104 | + |
| 105 | +func (css *ClusterSecretsService) Update(ctx context.Context, org, clusterID, secretID string, input ClusterSecretUpdate) (ClusterSecret, *Response, error) { |
| 106 | + u := fmt.Sprintf("v2/organizations/%s/clusters/%s/secrets/%s", org, clusterID, secretID) |
| 107 | + req, err := css.client.NewRequest(ctx, "PUT", u, input) |
| 108 | + if err != nil { |
| 109 | + return ClusterSecret{}, nil, err |
| 110 | + } |
| 111 | + |
| 112 | + var secret ClusterSecret |
| 113 | + resp, err := css.client.Do(req, &secret) |
| 114 | + if err != nil { |
| 115 | + return ClusterSecret{}, resp, err |
| 116 | + } |
| 117 | + |
| 118 | + return secret, resp, err |
| 119 | +} |
| 120 | + |
| 121 | +func (css *ClusterSecretsService) UpdateValue(ctx context.Context, org, clusterID, secretID string, input ClusterSecretValueUpdate) (*Response, error) { |
| 122 | + u := fmt.Sprintf("v2/organizations/%s/clusters/%s/secrets/%s/value", org, clusterID, secretID) |
| 123 | + req, err := css.client.NewRequest(ctx, "PUT", u, input) |
| 124 | + if err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + |
| 128 | + return css.client.Do(req, nil) |
| 129 | +} |
| 130 | + |
| 131 | +func (css *ClusterSecretsService) Delete(ctx context.Context, org, clusterID, secretID string) (*Response, error) { |
| 132 | + u := fmt.Sprintf("v2/organizations/%s/clusters/%s/secrets/%s", org, clusterID, secretID) |
| 133 | + req, err := css.client.NewRequest(ctx, "DELETE", u, nil) |
| 134 | + if err != nil { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + |
| 138 | + return css.client.Do(req, nil) |
| 139 | +} |
0 commit comments