Skip to content

Commit

Permalink
feat(monitor): ADD new cloud account creation endpoint (#565)
Browse files Browse the repository at this point in the history
* ADD new cloud account creation endpoint
  • Loading branch information
jaimeyh authored Dec 5, 2024
1 parent 6f601ca commit 380bde1
Show file tree
Hide file tree
Showing 4 changed files with 326 additions and 19 deletions.
85 changes: 85 additions & 0 deletions sysdig/internal/client/v2/cloud_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const (
cloudAccountPath = "%s/api/cloud/v2/accounts/%s"
cloudAccountWithExternalIDPath = "%s/api/cloud/v2/accounts/%s?includeExternalID=true"
providersPath = "%v/api/v2/providers"
costCloudAccountPath = "%s/api/cloudaccount"
costProviderURL = "%s/api/cloudaccount/features/cost/account?id=%d"
updateCostProviderURL = "%s/api/cloudaccount/features/cost"
)

type CloudAccountSecureInterface interface {
Expand All @@ -25,8 +28,11 @@ type CloudAccountSecureInterface interface {
type CloudAccountMonitorInterface interface {
Base
CreateCloudAccountMonitor(ctx context.Context, provider *CloudAccountMonitor) (*CloudAccountMonitor, error)
CreateCloudAccountMonitorForCost(ctx context.Context, provider *CloudAccountMonitorForCost) (*CloudAccountCreatedForCost, error)
UpdateCloudAccountMonitor(ctx context.Context, id int, provider *CloudAccountMonitor) (*CloudAccountMonitor, error)
UpdateCloudAccountMonitorForCost(ctx context.Context, provider *CloudAccountCostProvider) (*CloudAccountCostProvider, error)
GetCloudAccountMonitor(ctx context.Context, id int) (*CloudAccountMonitor, error)
GetCloudAccountMonitorForCost(ctx context.Context, id int) (*CloudAccountCostProvider, error)
DeleteCloudAccountMonitor(ctx context.Context, id int) error
}

Expand Down Expand Up @@ -135,6 +141,30 @@ func (client *Client) CreateCloudAccountMonitor(ctx context.Context, provider *C
return &wrapper.CloudAccount, nil
}

func (client *Client) CreateCloudAccountMonitorForCost(ctx context.Context, provider *CloudAccountMonitorForCost) (*CloudAccountCreatedForCost, error) {
payload, err := Marshal(provider)
if err != nil {
return nil, err
}

response, err := client.requester.Request(ctx, http.MethodPost, client.getCostProvidersURL(), payload)
if err != nil {
return nil, err
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
return nil, client.ErrorFromResponse(response)
}

wrapper, err := Unmarshal[CloudAccountCreatedForCost](response.Body)
if err != nil {
return nil, err
}

return &wrapper, nil
}

func (client *Client) UpdateCloudAccountMonitor(ctx context.Context, id int, provider *CloudAccountMonitor) (*CloudAccountMonitor, error) {
payload, err := Marshal(provider)
if err != nil {
Expand All @@ -159,6 +189,30 @@ func (client *Client) UpdateCloudAccountMonitor(ctx context.Context, id int, pro
return &wrapper.CloudAccount, nil
}

func (client *Client) UpdateCloudAccountMonitorForCost(ctx context.Context, provider *CloudAccountCostProvider) (*CloudAccountCostProvider, error) {
payload, err := Marshal(provider)
if err != nil {
return nil, err
}

response, err := client.requester.Request(ctx, http.MethodPut, client.getUpdateCostProviderURL(), payload)
if err != nil {
return nil, err
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
return nil, client.ErrorFromResponse(response)
}

wrapper, err := Unmarshal[CloudAccountCostProviderWrapper](response.Body)
if err != nil {
return nil, err
}

return &wrapper.CloudAccountCostProvider, nil
}

func (client *Client) GetCloudAccountMonitor(ctx context.Context, id int) (*CloudAccountMonitor, error) {
response, err := client.requester.Request(ctx, http.MethodGet, client.getProviderURL(id), nil)
if err != nil {
Expand All @@ -178,6 +232,25 @@ func (client *Client) GetCloudAccountMonitor(ctx context.Context, id int) (*Clou
return &wrapper.CloudAccount, nil
}

func (client *Client) GetCloudAccountMonitorForCost(ctx context.Context, id int) (*CloudAccountCostProvider, error) {
response, err := client.requester.Request(ctx, http.MethodGet, client.getCostProviderURL(id), nil)
if err != nil {
return nil, err
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
return nil, client.ErrorFromResponse(response)
}

wrapper, err := Unmarshal[CloudAccountCostProviderWrapper](response.Body)
if err != nil {
return nil, err
}

return &wrapper.CloudAccountCostProvider, nil
}

func (client *Client) DeleteCloudAccountMonitor(ctx context.Context, id int) error {
response, err := client.requester.Request(ctx, http.MethodDelete, client.getProviderURL(id), nil)
if err != nil {
Expand All @@ -199,3 +272,15 @@ func (client *Client) getProviderURL(id int) string {
func (client *Client) getProvidersURL() string {
return fmt.Sprintf(providersPath, client.config.url)
}

func (client *Client) getCostProvidersURL() string {
return fmt.Sprintf(costCloudAccountPath, client.config.url)
}

func (client *Client) getCostProviderURL(id int) string {
return fmt.Sprintf(costProviderURL, client.config.url, id)
}

func (client *Client) getUpdateCostProviderURL() string {
return fmt.Sprintf(updateCostProviderURL, client.config.url)
}
56 changes: 56 additions & 0 deletions sysdig/internal/client/v2/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -939,10 +939,66 @@ type CloudAccountMonitor struct {
AdditionalOptions string `json:"additionalOptions"`
}

type CloudAccountMonitorForCost struct {
Feature string `json:"feature"`
Platform string `json:"platform"`
Configuration CloudCostConfiguration `json:"config"`
Credentials CloudAccountCredentialsMonitor `json:"credentials"`
}

type CloudCostConfiguration struct {
AthenaBucketName string `json:"athenaBucketName"`
AthenaDatabaseName string `json:"athenaDatabaseName"`
AthenaRegion string `json:"athenaRegion"`
AthenaWorkgroup string `json:"athenaWorkgroup"`
AthenaTableName string `json:"athenaTableName"`
SpotPricesBucketName string `json:"spotPricesBucketName"`
}

type CloudAccountCreatedForCost struct {
Id string `json:"id"`
CustomerId int `json:"customerId"`
ProviderId string `json:"providerId"`
Provider string `json:"provider"`
SkipFetch bool `json:"skipFetch"`
IntegrationType string `json:"integrationType"`
CredentialsType string `json:"credentialsType"`
RoleArn string `json:"roleArn"`
ExternalId string `json:"externalId"`
}

type cloudAccountWrapperMonitor struct {
CloudAccount CloudAccountMonitor `json:"provider"`
}

type CloudConfigForCost struct {
AthenaProjectId string `json:"athenaProjectId"`
AthenaBucketName string `json:"athenaBucketName"`
AthenaRegion string `json:"athenaRegion"`
AthenaDatabaseName string `json:"athenaDatabaseName"`
AthenaTableName string `json:"athenaTableName"`
AthenaWorkgroup string `json:"athenaWorkgroup"`
SpotPricesBucketName string `json:"spotPricesBucketName"`
IntegrationType string `json:"integrationType"`
}

type CloudAccountCostProvider struct {
CustomerId int `json:"customerId"`
ProviderId string `json:"providerId"`
Provider string `json:"provider"`
CredentialsId string `json:"credentialsId"`
Feature string `json:"feature"`
Config CloudConfigForCost `json:"config"`
Enabled bool `json:"enabled"`
CredentialsType string `json:"credentialsType"`
RoleArn string `json:"roleArn"`
ExternalId string `json:"externalId"`
}

type CloudAccountCostProviderWrapper struct {
CloudAccountCostProvider CloudAccountCostProvider `json:"item"`
}

type PosturePolicyZoneMeta struct {
ID string `json:"id"`
Name string `json:"name"`
Expand Down
Loading

0 comments on commit 380bde1

Please sign in to comment.