|
| 1 | +package v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + cloudauthAccountComponentsPath = "%s/api/cloudauth/v1/accounts/%s/components" // POST |
| 11 | + cloudauthAccountComponentPath = "%s/api/cloudauth/v1/accounts/%s/components/%s/%s" // GET, PUT, DEL |
| 12 | + // getCloudauthAccountPath = "%s/api/cloudauth/v1/accounts/%s?decrypt=%s" // does GET require decryption? |
| 13 | +) |
| 14 | + |
| 15 | +type CloudauthAccountComponentSecureInterface interface { |
| 16 | + Base |
| 17 | + CreateCloudauthAccountComponentSecure(ctx context.Context, accountID string, cloudAccountComponent *CloudauthAccountComponentSecure) (*CloudauthAccountComponentSecure, string, error) |
| 18 | + GetCloudauthAccountComponentSecure(ctx context.Context, accountID, componentType, componentInstance string) (*CloudauthAccountComponentSecure, string, error) |
| 19 | + DeleteCloudauthAccountComponentSecure(ctx context.Context, accountID, componentType, componentInstance string) (string, error) |
| 20 | + UpdateCloudauthAccountComponentSecure(ctx context.Context, accountID, componentType, componentInstance string, cloudAccountComponent *CloudauthAccountComponentSecure) (*CloudauthAccountComponentSecure, string, error) |
| 21 | +} |
| 22 | + |
| 23 | +func (client *Client) CreateCloudauthAccountComponentSecure(ctx context.Context, accountID string, cloudAccountComponent *CloudauthAccountComponentSecure) (*CloudauthAccountComponentSecure, string, error) { |
| 24 | + payload, err := client.marshalCloudauthProto(cloudAccountComponent) |
| 25 | + if err != nil { |
| 26 | + return nil, "", err |
| 27 | + } |
| 28 | + |
| 29 | + response, err := client.requester.Request(ctx, http.MethodPost, client.cloudauthAccountComponentsURL(accountID), payload) |
| 30 | + if err != nil { |
| 31 | + return nil, "", err |
| 32 | + } |
| 33 | + defer response.Body.Close() |
| 34 | + |
| 35 | + if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated { |
| 36 | + errStatus, err := client.ErrorAndStatusFromResponse(response) |
| 37 | + return nil, errStatus, err |
| 38 | + } |
| 39 | + |
| 40 | + cloudauthAccountComponent := &CloudauthAccountComponentSecure{} |
| 41 | + err = client.unmarshalCloudauthProto(response.Body, cloudauthAccountComponent) |
| 42 | + if err != nil { |
| 43 | + return nil, "", err |
| 44 | + } |
| 45 | + return cloudauthAccountComponent, "", nil |
| 46 | +} |
| 47 | + |
| 48 | +func (client *Client) GetCloudauthAccountComponentSecure(ctx context.Context, accountID, componentType, componentInstance string) (*CloudauthAccountComponentSecure, string, error) { |
| 49 | + response, err := client.requester.Request(ctx, http.MethodGet, client.cloudauthAccountComponentURL(accountID, componentType, componentInstance), nil) |
| 50 | + if err != nil { |
| 51 | + return nil, "", err |
| 52 | + } |
| 53 | + defer response.Body.Close() |
| 54 | + |
| 55 | + if response.StatusCode != http.StatusOK { |
| 56 | + errStatus, err := client.ErrorAndStatusFromResponse(response) |
| 57 | + return nil, errStatus, err |
| 58 | + } |
| 59 | + |
| 60 | + cloudauthAccountComponent := &CloudauthAccountComponentSecure{} |
| 61 | + err = client.unmarshalCloudauthProto(response.Body, cloudauthAccountComponent) |
| 62 | + if err != nil { |
| 63 | + return nil, "", err |
| 64 | + } |
| 65 | + return cloudauthAccountComponent, "", nil |
| 66 | +} |
| 67 | + |
| 68 | +func (client *Client) DeleteCloudauthAccountComponentSecure(ctx context.Context, accountID, componentType, componentInstance string) (string, error) { |
| 69 | + response, err := client.requester.Request(ctx, http.MethodDelete, client.cloudauthAccountComponentURL(accountID, componentType, componentInstance), nil) |
| 70 | + if err != nil { |
| 71 | + return "", err |
| 72 | + } |
| 73 | + defer response.Body.Close() |
| 74 | + |
| 75 | + if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK { |
| 76 | + return client.ErrorAndStatusFromResponse(response) |
| 77 | + } |
| 78 | + return "", nil |
| 79 | +} |
| 80 | + |
| 81 | +func (client *Client) UpdateCloudauthAccountComponentSecure(ctx context.Context, accountID, componentType, componentInstance string, cloudAccountComponent *CloudauthAccountComponentSecure) ( |
| 82 | + *CloudauthAccountComponentSecure, string, error) { |
| 83 | + payload, err := client.marshalCloudauthProto(cloudAccountComponent) |
| 84 | + if err != nil { |
| 85 | + return nil, "", err |
| 86 | + } |
| 87 | + |
| 88 | + response, err := client.requester.Request(ctx, http.MethodPut, client.cloudauthAccountComponentURL(accountID, componentType, componentInstance), payload) |
| 89 | + if err != nil { |
| 90 | + return nil, "", err |
| 91 | + } |
| 92 | + defer response.Body.Close() |
| 93 | + |
| 94 | + if response.StatusCode != http.StatusOK { |
| 95 | + errStatus, err := client.ErrorAndStatusFromResponse(response) |
| 96 | + return nil, errStatus, err |
| 97 | + } |
| 98 | + |
| 99 | + cloudauthAccountComponent := &CloudauthAccountComponentSecure{} |
| 100 | + err = client.unmarshalCloudauthProto(response.Body, cloudauthAccountComponent) |
| 101 | + if err != nil { |
| 102 | + return nil, "", err |
| 103 | + } |
| 104 | + return cloudauthAccountComponent, "", nil |
| 105 | +} |
| 106 | + |
| 107 | +func (client *Client) cloudauthAccountComponentsURL(accountID string) string { |
| 108 | + return fmt.Sprintf(cloudauthAccountComponentsPath, client.config.url, accountID) |
| 109 | +} |
| 110 | + |
| 111 | +func (client *Client) cloudauthAccountComponentURL(accountID string, componentType string, componentInstance string) string { |
| 112 | + return fmt.Sprintf(cloudauthAccountComponentPath, client.config.url, accountID, componentType, componentInstance) |
| 113 | +} |
0 commit comments