Skip to content

Commit

Permalink
Remove redundant client implementation between create & update
Browse files Browse the repository at this point in the history
  • Loading branch information
ravinadhruve10 authored and cgeers committed May 2, 2024
1 parent dc9ea77 commit 7bc154a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 33 deletions.
36 changes: 5 additions & 31 deletions sysdig/internal/client/v2/cloudauth_account_feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ const (

type CloudauthAccountFeatureSecureInterface interface {
Base
CreateCloudauthAccountFeatureSecure(ctx context.Context, accountID string, cloudAccountFeature *CloudauthAccountFeatureSecure) (*CloudauthAccountFeatureSecure, string, error)
CreateOrUpdateCloudauthAccountFeatureSecure(ctx context.Context, accountID, featureType string, cloudAccountFeature *CloudauthAccountFeatureSecure) (*CloudauthAccountFeatureSecure, string, error)
GetCloudauthAccountFeatureSecure(ctx context.Context, accountID, featureType string) (*CloudauthAccountFeatureSecure, string, error)
DeleteCloudauthAccountFeatureSecure(ctx context.Context, accountID, featureType string) (string, error)
UpdateCloudauthAccountFeatureSecure(ctx context.Context, accountID, featureType string, cloudAccountFeature *CloudauthAccountFeatureSecure) (*CloudauthAccountFeatureSecure, string, error)
}

// create method acts as a PUT call to backend
func (client *Client) CreateCloudauthAccountFeatureSecure(ctx context.Context, accountID string, cloudAccountFeature *CloudauthAccountFeatureSecure) (*CloudauthAccountFeatureSecure, string, error) {
// both create and update makes a PUT call to backend
func (client *Client) CreateOrUpdateCloudauthAccountFeatureSecure(ctx context.Context, accountID, featureType string, cloudAccountFeature *CloudauthAccountFeatureSecure) (
*CloudauthAccountFeatureSecure, string, error) {
payload, err := client.marshalCloudauthProto(cloudAccountFeature)
if err != nil {
return nil, "", err
}

response, err := client.requester.Request(ctx, http.MethodPut, client.cloudauthAccountFeatureURL(accountID, cloudAccountFeature.AccountFeature.Type.String()), payload)
response, err := client.requester.Request(ctx, http.MethodPut, client.cloudauthAccountFeatureURL(accountID, featureType), payload)
if err != nil {
return nil, "", err
}
Expand Down Expand Up @@ -77,32 +77,6 @@ func (client *Client) DeleteCloudauthAccountFeatureSecure(ctx context.Context, a
return "", nil
}

func (client *Client) UpdateCloudauthAccountFeatureSecure(ctx context.Context, accountID, featureType string, cloudAccountFeature *CloudauthAccountFeatureSecure) (
*CloudauthAccountFeatureSecure, string, error) {
payload, err := client.marshalCloudauthProto(cloudAccountFeature)
if err != nil {
return nil, "", err
}

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

if response.StatusCode != http.StatusOK {
errStatus, err := client.ErrorAndStatusFromResponse(response)
return nil, errStatus, err
}

cloudauthAccountFeature := &CloudauthAccountFeatureSecure{}
err = client.unmarshalCloudauthProto(response.Body, cloudauthAccountFeature)
if err != nil {
return nil, "", err
}
return cloudauthAccountFeature, "", nil
}

func (client *Client) cloudauthAccountFeatureURL(accountID string, featureType string) string {
return fmt.Sprintf(cloudauthAccountFeaturePath, client.config.url, accountID, featureType)
}
5 changes: 3 additions & 2 deletions sysdig/resource_sysdig_secure_cloud_auth_account_feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ func resourceSysdigSecureCloudauthAccountFeatureCreate(ctx context.Context, data
}

accountId := data.Get(SchemaAccountId).(string)
cloudauthAccountFeature, errStatus, err := client.CreateCloudauthAccountFeatureSecure(ctx, accountId, cloudauthAccountFeatureFromResourceData(data))
cloudauthAccountFeature, errStatus, err := client.CreateOrUpdateCloudauthAccountFeatureSecure(
ctx, accountId, data.Get(SchemaType).(string), cloudauthAccountFeatureFromResourceData(data))
if err != nil {
return diag.Errorf("Error creating resource: %s %s", errStatus, err)
}
Expand Down Expand Up @@ -140,7 +141,7 @@ func resourceSysdigSecureCloudauthAccountFeatureUpdate(ctx context.Context, data
return diag.Errorf("Error updating resource: %s", err)
}

_, errStatus, err = client.UpdateCloudauthAccountFeatureSecure(
_, errStatus, err = client.CreateOrUpdateCloudauthAccountFeatureSecure(
ctx, accountId, data.Get(SchemaType).(string), newCloudAccountFeature)
if err != nil {
if strings.Contains(errStatus, "404") {
Expand Down

0 comments on commit 7bc154a

Please sign in to comment.