Skip to content

Commit 1f5c356

Browse files
authored
refactor(cloudaccount): refactor cloud account in secure to v2 (#318)
* refactor(cloudaccount): refactor cloud account in secure to v2
1 parent 4b2db5c commit 1f5c356

11 files changed

+156
-299
lines changed

sysdig/data_source_sysdig_secure_trusted_cloud_identity.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ func dataSourceSysdigSecureTrustedCloudIdentity() *schema.Resource {
5353

5454
// Retrieves the information of a resource form the file and loads it in Terraform
5555
func dataSourceSysdigSecureTrustedCloudIdentityRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
56-
client, err := meta.(SysdigClients).sysdigSecureClient()
56+
client, err := getSecureCloudAccountClient(meta.(SysdigClients))
5757
if err != nil {
5858
return diag.FromErr(err)
5959
}
6060

61-
identity, err := client.GetTrustedCloudIdentity(ctx, d.Get("cloud_provider").(string))
61+
identity, err := client.GetTrustedCloudIdentitySecure(ctx, d.Get("cloud_provider").(string))
6262
if err != nil {
6363
return diag.FromErr(err)
6464
}

sysdig/internal/client/secure/client.go

Lines changed: 0 additions & 71 deletions
This file was deleted.

sysdig/internal/client/secure/cloud_account.go

Lines changed: 0 additions & 114 deletions
This file was deleted.

sysdig/internal/client/secure/helpers.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

sysdig/internal/client/secure/models.go

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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

Comments
 (0)