|
| 1 | +package v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + cloudAccountsPath = "%s/api/cloud/v2/accounts" |
| 11 | + cloudAccountsWithExternalIDPath = "%s/api/cloud/v2/accounts?includeExternalID=true&upsert=true" |
| 12 | + cloudAccountPath = "%s/api/cloud/v2/accounts/%s" |
| 13 | + cloudAccountWithExternalIDPath = "%s/api/cloud/v2/accounts/%s?includeExternalID=true" |
| 14 | + trustedCloudIdentityPath = "%s/api/cloud/v2/%s/trustedIdentity" |
| 15 | +) |
| 16 | + |
| 17 | +type CloudAccountSecureInterface interface { |
| 18 | + Base |
| 19 | + CreateCloudAccountSecure(ctx context.Context, cloudAccount *CloudAccountSecure) (*CloudAccountSecure, error) |
| 20 | + GetCloudAccountSecure(ctx context.Context, accountID string) (*CloudAccountSecure, error) |
| 21 | + DeleteCloudAccountSecure(ctx context.Context, accountID string) error |
| 22 | + UpdateCloudAccountSecure(ctx context.Context, accountID string, cloudAccount *CloudAccountSecure) (*CloudAccountSecure, error) |
| 23 | + GetTrustedCloudIdentitySecure(ctx context.Context, provider string) (string, error) |
| 24 | +} |
| 25 | + |
| 26 | +func (client *Client) CreateCloudAccountSecure(ctx context.Context, cloudAccount *CloudAccountSecure) (*CloudAccountSecure, error) { |
| 27 | + payload, err := Marshal(cloudAccount) |
| 28 | + if err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + |
| 32 | + response, err := client.requester.Request(ctx, http.MethodPost, client.cloudAccountsURL(true), payload) |
| 33 | + if err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + defer response.Body.Close() |
| 37 | + |
| 38 | + if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated { |
| 39 | + err = client.ErrorFromResponse(response) |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + |
| 43 | + return Unmarshal[*CloudAccountSecure](response.Body) |
| 44 | +} |
| 45 | + |
| 46 | +func (client *Client) GetCloudAccountSecure(ctx context.Context, accountID string) (*CloudAccountSecure, error) { |
| 47 | + response, err := client.requester.Request(ctx, http.MethodGet, client.cloudAccountURL(accountID, true), nil) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + defer response.Body.Close() |
| 52 | + |
| 53 | + if response.StatusCode != http.StatusOK { |
| 54 | + return nil, client.ErrorFromResponse(response) |
| 55 | + } |
| 56 | + |
| 57 | + return Unmarshal[*CloudAccountSecure](response.Body) |
| 58 | +} |
| 59 | + |
| 60 | +func (client *Client) DeleteCloudAccountSecure(ctx context.Context, accountID string) error { |
| 61 | + response, err := client.requester.Request(ctx, http.MethodDelete, client.cloudAccountURL(accountID, false), nil) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + defer response.Body.Close() |
| 66 | + |
| 67 | + if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK { |
| 68 | + return client.ErrorFromResponse(response) |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func (client *Client) UpdateCloudAccountSecure(ctx context.Context, accountID string, cloudAccount *CloudAccountSecure) (*CloudAccountSecure, error) { |
| 74 | + payload, err := Marshal(cloudAccount) |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + response, err := client.requester.Request(ctx, http.MethodPut, client.cloudAccountURL(accountID, true), payload) |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + defer response.Body.Close() |
| 84 | + |
| 85 | + if response.StatusCode != http.StatusOK { |
| 86 | + err = client.ErrorFromResponse(response) |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + |
| 90 | + return Unmarshal[*CloudAccountSecure](response.Body) |
| 91 | +} |
| 92 | + |
| 93 | +func (client *Client) GetTrustedCloudIdentitySecure(ctx context.Context, provider string) (string, error) { |
| 94 | + response, err := client.requester.Request(ctx, http.MethodGet, client.trustedCloudIdentityURL(provider), nil) |
| 95 | + if err != nil { |
| 96 | + return "", err |
| 97 | + } |
| 98 | + defer response.Body.Close() |
| 99 | + |
| 100 | + if response.StatusCode != http.StatusOK { |
| 101 | + return "", client.ErrorFromResponse(response) |
| 102 | + } |
| 103 | + |
| 104 | + return Unmarshal[string](response.Body) |
| 105 | +} |
| 106 | + |
| 107 | +func (client *Client) cloudAccountsURL(includeExternalID bool) string { |
| 108 | + if includeExternalID { |
| 109 | + return fmt.Sprintf(cloudAccountsWithExternalIDPath, client.config.url) |
| 110 | + } |
| 111 | + return fmt.Sprintf(cloudAccountsPath, client.config.url) |
| 112 | +} |
| 113 | + |
| 114 | +func (client *Client) cloudAccountURL(accountID string, includeExternalID bool) string { |
| 115 | + if includeExternalID { |
| 116 | + return fmt.Sprintf(cloudAccountWithExternalIDPath, client.config.url, accountID) |
| 117 | + } |
| 118 | + return fmt.Sprintf(cloudAccountPath, client.config.url, accountID) |
| 119 | +} |
| 120 | + |
| 121 | +func (client *Client) trustedCloudIdentityURL(provider string) string { |
| 122 | + return fmt.Sprintf(trustedCloudIdentityPath, client.config.url, provider) |
| 123 | +} |
0 commit comments