Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Tom Harvey <tombuildsstuff@users.noreply.github.com>
  • Loading branch information
jan-mrm and tombuildsstuff authored Mar 15, 2024
1 parent 2c9bd6a commit b1dcfc8
Showing 1 changed file with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func resourceApiManagementPolicyFragment() *pluginsdk.Resource {
Update: resourceApiManagementPolicyFragmentUpdate,
Delete: resourceApiManagementPolicyFragmentDelete,
Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error {
_, err := policyfragment.ParsePolicyFragmentIDInsensitively(id)
_, err := policyfragment.ParsePolicyFragmentID(id)
return err
}, func(ctx context.Context, d *pluginsdk.ResourceData, meta interface{}) ([]*pluginsdk.ResourceData, error) {
client := meta.(*clients.Client).ApiManagement.PolicyFragmentClient

id, err := policyfragment.ParsePolicyFragmentIDInsensitively(d.Id())
id, err := policyfragment.ParsePolicyFragmentID(d.Id())
if err != nil {
return nil, err
}
Expand All @@ -49,7 +49,7 @@ func resourceApiManagementPolicyFragment() *pluginsdk.Resource {
return nil, fmt.Errorf("retrieving Api Management Policy Fragment %q (Api Management: %q, Resource Group %q): %+v", id.PolicyFragmentName, id.ServiceName, id.ResourceGroupName, err)
}

d.Set("format", policyfragment.PolicyFragmentContentFormatXml)
d.Set("format", string(policyfragment.PolicyFragmentContentFormatXml))

return []*pluginsdk.ResourceData{d}, nil
}),
Expand All @@ -64,9 +64,7 @@ func resourceApiManagementPolicyFragment() *pluginsdk.Resource {
Schema: map[string]*pluginsdk.Schema{
"name": schemaz.SchemaApiManagementChildName(),

"resource_group_name": commonschema.ResourceGroupName(),

"api_management_name": schemaz.SchemaApiManagementName(),
"api_management_id": commonschema.ResourceIDReferenceRequiredForceNew(&apimanagementservice.ServiceId{}),

Check failure on line 67 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / tflint

undefined: apimanagementservice

Check failure on line 67 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / tflint

undefined: apimanagementservice

Check failure on line 67 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / detect

undefined: apimanagementservice

Check failure on line 67 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / document-lint

undefined: apimanagementservice

Check failure on line 67 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / golint

undefined: apimanagementservice

Check failure on line 67 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / compatibility-32bit-test

undefined: apimanagementservice

"format": {
Type: pluginsdk.TypeString,
Expand Down Expand Up @@ -95,16 +93,20 @@ func resourceApiManagementPolicyFragment() *pluginsdk.Resource {

func resourceApiManagementPolicyFragmentCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).ApiManagement.PolicyFragmentClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
subscriptionId := meta.(*clients.Client).Account.SubscriptionId

Check failure on line 97 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / tflint

subscriptionId declared and not used

Check failure on line 97 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / detect

subscriptionId declared and not used

Check failure on line 97 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / document-lint

subscriptionId declared and not used

Check failure on line 97 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / golint

subscriptionId declared and not used

Check failure on line 97 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / compatibility-32bit-test

subscriptionId declared and not used
defer cancel()

id := policyfragment.NewPolicyFragmentID(subscriptionId, d.Get("resource_group_name").(string), d.Get("api_management_name").(string), d.Get("name").(string))
apiManagementId, err := apimanagementservice.ParseServiceID(d.Get("api_management_id").(string))

Check failure on line 100 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / tflint

undefined: apimanagementservice

Check failure on line 100 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / detect

undefined: apimanagementservice

Check failure on line 100 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / document-lint

undefined: apimanagementservice

Check failure on line 100 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / golint

undefined: apimanagementservice

Check failure on line 100 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / compatibility-32bit-test

undefined: apimanagementservice
if err != nil {
return err
}
id := policyfragment.NewPolicyFragmentID(apiManagementId.SubscriptionID, apiManagementId.ResourceGroupName, apiManagementId.ServiceName, d.Get("name").(string))
format := policyfragment.PolicyFragmentContentFormat(d.Get("format").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, id, policyfragment.GetOperationOptions{
Format: &format,
})
opts := policyfragment.DefaultGetOperationOptions
opts.Format = &format

Check failure on line 108 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / tflint

opts.Format undefined (type func() policyfragment.GetOperationOptions has no field or method Format)

Check failure on line 108 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / detect

opts.Format undefined (type func() policyfragment.GetOperationOptions has no field or method Format)

Check failure on line 108 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / document-lint

opts.Format undefined (type func() policyfragment.GetOperationOptions has no field or method Format)

Check failure on line 108 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / golint

opts.Format undefined (type func() policyfragment.GetOperationOptions has no field or method Format)

Check failure on line 108 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / compatibility-32bit-test

opts.Format undefined (type func() policyfragment.GetOperationOptions has no field or method Format)
existing, err := client.Get(ctx, id, opts)

Check failure on line 109 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / tflint

cannot use opts (variable of type func() policyfragment.GetOperationOptions) as policyfragment.GetOperationOptions value in argument to client.Get

Check failure on line 109 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / detect

cannot use opts (variable of type func() policyfragment.GetOperationOptions) as policyfragment.GetOperationOptions value in argument to client.Get

Check failure on line 109 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / document-lint

cannot use opts (variable of type func() policyfragment.GetOperationOptions) as policyfragment.GetOperationOptions value in argument to client.Get

Check failure on line 109 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / golint

cannot use opts (variable of type func() policyfragment.GetOperationOptions) as policyfragment.GetOperationOptions value in argument to client.Get

Check failure on line 109 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / compatibility-32bit-test

cannot use opts (variable of type func() policyfragment.GetOperationOptions) as policyfragment.GetOperationOptions value in argument to client.Get
if err != nil {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
Expand Down Expand Up @@ -138,10 +140,10 @@ func resourceApiManagementPolicyFragmentCreate(d *pluginsdk.ResourceData, meta i

func resourceApiManagementPolicyFragmentUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).ApiManagement.PolicyFragmentClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := policyfragment.ParsePolicyFragmentIDInsensitively(d.Get("id").(string))
id, err := policyfragment.ParsePolicyFragmentID(d.ID())

Check failure on line 146 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / tflint

d.ID undefined (type *"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema".ResourceData has no field or method ID)

Check failure on line 146 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / detect

d.ID undefined (type *"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema".ResourceData has no field or method ID)

Check failure on line 146 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / document-lint

d.ID undefined (type *"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema".ResourceData has no field or method ID)

Check failure on line 146 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / golint

d.ID undefined (type *"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema".ResourceData has no field or method ID)

Check failure on line 146 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / compatibility-32bit-test

d.ID undefined (type *"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema".ResourceData has no field or method ID)
if err != nil {
return err
}
Expand Down Expand Up @@ -173,15 +175,15 @@ func resourceApiManagementPolicyFragmentRead(d *pluginsdk.ResourceData, meta int
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := policyfragment.ParsePolicyFragmentIDInsensitively(d.Id())
id, err := policyfragment.ParsePolicyFragmentID(d.Id())
if err != nil {
return err
}

format := policyfragment.PolicyFragmentContentFormat(d.Get("format").(string))
resp, err := client.Get(ctx, *id, policyfragment.GetOperationOptions{
Format: &format,
})
opts := policyfragment.DefaultGetOperationOptions()
opts.Format = &format
resp, err := client.Get(ctx, *id, opts)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
log.Printf("[DEBUG] %s does not exist - removing from state!", *id)
Expand All @@ -193,19 +195,19 @@ func resourceApiManagementPolicyFragmentRead(d *pluginsdk.ResourceData, meta int
}

d.Set("name", id.PolicyFragmentName)
d.Set("resource_group_name", id.ResourceGroupName)
d.Set("api_management_name", id.ServiceName)
apiManagementId := apimanagementservice.NewServiceID(id.SubscriptionID, id.ResourceGroupName, id.ServiceName)

Check failure on line 198 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / tflint

undefined: apimanagementservice

Check failure on line 198 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / tflint

id.SubscriptionID undefined (type *policyfragment.PolicyFragmentId has no field or method SubscriptionID)

Check failure on line 198 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / detect

undefined: apimanagementservice

Check failure on line 198 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / detect

id.SubscriptionID undefined (type *policyfragment.PolicyFragmentId has no field or method SubscriptionID)

Check failure on line 198 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / document-lint

undefined: apimanagementservice

Check failure on line 198 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / document-lint

id.SubscriptionID undefined (type *policyfragment.PolicyFragmentId has no field or method SubscriptionID)

Check failure on line 198 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / golint

undefined: apimanagementservice

Check failure on line 198 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / golint

id.SubscriptionID undefined (type *policyfragment.PolicyFragmentId has no field or method SubscriptionID)

Check failure on line 198 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / compatibility-32bit-test

undefined: apimanagementservice

Check failure on line 198 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / compatibility-32bit-test

id.SubscriptionID undefined (type *policyfragment.PolicyFragmentId has no field or method SubscriptionID)
d.Set("api_management_id", apiManagementId.ID())

if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
d.Set("description", pointer.From(props.Description))
d.Set("value", props.Value)
// the api only returns a format field when it's requested in the GET request as param '?format=rawxml' and only does so when it's not 'xml'
if props.Format == nil {
d.Set("format", "xml")
} else {
d.Set("format", string(pointer.From(props.Format)))
format := policyfragment.PolicyFragmentContentFormatXml
if props.Format != nil{
format = *props.Format
}
d.Set("format", string(format))
}
}

Expand All @@ -217,15 +219,13 @@ func resourceApiManagementPolicyFragmentDelete(d *pluginsdk.ResourceData, meta i
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := policyfragment.ParsePolicyFragmentIDInsensitively(d.Id())
id, err := policyfragment.ParsePolicyFragmentID(d.Id())
if err != nil {
return err
}

if resp, err := client.Delete(ctx, *id, policyfragment.DeleteOperationOptions{}); err != nil {
if !response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("deleting %s: %s", id, err)
}
if err := client.Delete(ctx, *id, policyfragment.DefaultDeleteOperationOptions()); err != nil {

Check failure on line 227 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / tflint

assignment mismatch: 1 variable but client.Delete returns 2 values

Check failure on line 227 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / detect

assignment mismatch: 1 variable but client.Delete returns 2 values

Check failure on line 227 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / document-lint

assignment mismatch: 1 variable but client.Delete returns 2 values

Check failure on line 227 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / golint

assignment mismatch: 1 variable but client.Delete returns 2 values (typecheck)

Check failure on line 227 in internal/services/apimanagement/api_management_policy_fragment_resource.go

View workflow job for this annotation

GitHub Actions / compatibility-32bit-test

assignment mismatch: 1 variable but client.Delete returns 2 values
return fmt.Errorf("deleting %s: %s", *id, err)
}

return nil
Expand Down

0 comments on commit b1dcfc8

Please sign in to comment.