From 7bc154a0c5e9bb444911a9ecc482f9797a3f5d1d Mon Sep 17 00:00:00 2001 From: Ravina Dhruve Date: Wed, 1 May 2024 15:27:35 -0700 Subject: [PATCH] Remove redundant client implementation between create & update --- .../client/v2/cloudauth_account_feature.go | 36 +++---------------- ...ysdig_secure_cloud_auth_account_feature.go | 5 +-- 2 files changed, 8 insertions(+), 33 deletions(-) diff --git a/sysdig/internal/client/v2/cloudauth_account_feature.go b/sysdig/internal/client/v2/cloudauth_account_feature.go index 68a9a0d4..b6d00cc9 100644 --- a/sysdig/internal/client/v2/cloudauth_account_feature.go +++ b/sysdig/internal/client/v2/cloudauth_account_feature.go @@ -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 } @@ -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) } diff --git a/sysdig/resource_sysdig_secure_cloud_auth_account_feature.go b/sysdig/resource_sysdig_secure_cloud_auth_account_feature.go index 5c09ba45..d6f2d8b7 100644 --- a/sysdig/resource_sysdig_secure_cloud_auth_account_feature.go +++ b/sysdig/resource_sysdig_secure_cloud_auth_account_feature.go @@ -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) } @@ -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") {