Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

customproviders - switch to hashicorp/go-azure-sdk #18978

Merged
merged 1 commit into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/google/go-cmp v0.5.8
github.com/google/uuid v1.1.2
github.com/hashicorp/go-azure-helpers v0.45.0
github.com/hashicorp/go-azure-sdk v0.20221024.1162143
github.com/hashicorp/go-azure-sdk v0.20221025.1113207
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/go-version v1.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv
github.com/hashicorp/go-azure-helpers v0.12.0/go.mod h1:Zc3v4DNeX6PDdy7NljlYpnrdac1++qNW0I4U+ofGwpg=
github.com/hashicorp/go-azure-helpers v0.45.0 h1:xuht3X0L5S8ma5HR6IY+jvzkkLwvQ9ifDmpVGUcOB8s=
github.com/hashicorp/go-azure-helpers v0.45.0/go.mod h1:WiJNl0fD6PoM/MYuGTZ8yuzIaXQR3m2H2g6+EJ8nSwc=
github.com/hashicorp/go-azure-sdk v0.20221024.1162143 h1:YlckjIw3ztpFH9GSkmuQJxKnRPnUoSaOl2EuyjlOgtA=
github.com/hashicorp/go-azure-sdk v0.20221024.1162143/go.mod h1:BEjoURzcFwd+K3MqkbOt9jArIIrsqpBQ2Gz6DdemFIs=
github.com/hashicorp/go-azure-sdk v0.20221025.1113207 h1:6aFDSayJrCI6MoOLBV1bzvjo2R7KVzynVeOboQcVPa4=
github.com/hashicorp/go-azure-sdk v0.20221025.1113207/go.mod h1:BEjoURzcFwd+K3MqkbOt9jArIIrsqpBQ2Gz6DdemFIs=
github.com/hashicorp/go-checkpoint v0.5.0 h1:MFYpPZCnQqQTE18jFwSII6eUQrD/oxMFp3mlgcqk5mU=
github.com/hashicorp/go-checkpoint v0.5.0/go.mod h1:7nfLNL10NsxqO4iWuW6tWW0HjZuDrwkBuEQsVcpCOgg=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
Expand Down
6 changes: 3 additions & 3 deletions internal/services/customproviders/client/client.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package client

import (
"github.com/Azure/azure-sdk-for-go/services/preview/customproviders/mgmt/2018-09-01-preview/customproviders"
"github.com/hashicorp/go-azure-sdk/resource-manager/customproviders/2018-09-01-preview/customresourceprovider"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
CustomProviderClient *customproviders.CustomResourceProviderClient
CustomProviderClient *customresourceprovider.CustomResourceProviderClient
}

func NewClient(o *common.ClientOptions) *Client {
CustomProviderClient := customproviders.NewCustomResourceProviderClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
CustomProviderClient := customresourceprovider.NewCustomResourceProviderClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&CustomProviderClient.Client, o.ResourceManagerAuthorizer)

return &Client{
Expand Down
143 changes: 62 additions & 81 deletions internal/services/customproviders/custom_provider_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/customproviders/mgmt/2018-09-01-preview/customproviders"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/customproviders/2018-09-01-preview/customresourceprovider"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/customproviders/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/customproviders/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
Expand All @@ -25,7 +26,7 @@ func resourceCustomProvider() *pluginsdk.Resource {
Update: resourceCustomProviderCreateUpdate,
Delete: resourceCustomProviderDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.ResourceProviderID(id)
_, err := customresourceprovider.ParseResourceProviderID(id)
return err
}),

Expand Down Expand Up @@ -111,7 +112,7 @@ func resourceCustomProvider() *pluginsdk.Resource {
},
},

"tags": tags.ForceNewSchema(),
"tags": commonschema.TagsForceNew(),
},
}
}
Expand All @@ -123,40 +124,35 @@ func resourceCustomProviderCreateUpdate(d *pluginsdk.ResourceData, meta interfac
defer cancel()

location := azure.NormalizeLocation(d.Get("location").(string))
id := parse.NewResourceProviderID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
id := customresourceprovider.NewResourceProviderID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.Name)
existing, err := client.Get(ctx, id)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
}
}

if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError("azurerm_custom_resource_provider", id.ID())
}
}

provider := customproviders.CustomRPManifest{
CustomRPManifestProperties: &customproviders.CustomRPManifestProperties{
provider := customresourceprovider.CustomRPManifest{
Properties: &customresourceprovider.CustomRPManifestProperties{
ResourceTypes: expandCustomProviderResourceType(d.Get("resource_type").(*pluginsdk.Set).List()),
Actions: expandCustomProviderAction(d.Get("action").(*pluginsdk.Set).List()),
Validations: expandCustomProviderValidation(d.Get("validation").(*pluginsdk.Set).List()),
},
Location: &location,
Location: location,
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, provider)
if err != nil {
if err := client.CreateOrUpdateThenPoll(ctx, id, provider); err != nil {
return fmt.Errorf("creating/updating %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for completion of %s: %+v", id, err)
}

d.SetId(id.ID())
return resourceCustomProviderRead(d, meta)
}
Expand All @@ -166,87 +162,85 @@ func resourceCustomProviderRead(d *pluginsdk.ResourceData, meta interface{}) err
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ResourceProviderID(d.Id())
id, err := customresourceprovider.ParseResourceProviderID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
resp, err := client.Get(ctx, *id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
d.SetId("")
return nil
}

return fmt.Errorf("retrieving Custom Provider %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", id.ResourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
d.Set("name", id.ResourceProviderName)
d.Set("resource_group_name", id.ResourceGroupName)

if err := d.Set("resource_type", flattenCustomProviderResourceType(resp.ResourceTypes)); err != nil {
return fmt.Errorf("setting `resource_type`: %+v", err)
}
if model := resp.Model; model != nil {
d.Set("location", azure.NormalizeLocation(model.Location))

if err := d.Set("action", flattenCustomProviderAction(resp.Actions)); err != nil {
return fmt.Errorf("setting `action`: %+v", err)
}
if props := model.Properties; props != nil {
if err := d.Set("resource_type", flattenCustomProviderResourceType(props.ResourceTypes)); err != nil {
return fmt.Errorf("setting `resource_type`: %+v", err)
}

if err := d.Set("validation", flattenCustomProviderValidation(resp.Validations)); err != nil {
return fmt.Errorf("setting `validation`: %+v", err)
}
if err := d.Set("action", flattenCustomProviderAction(props.Actions)); err != nil {
return fmt.Errorf("setting `action`: %+v", err)
}

return tags.FlattenAndSet(d, resp.Tags)
if err := d.Set("validation", flattenCustomProviderValidation(props.Validations)); err != nil {
return fmt.Errorf("setting `validation`: %+v", err)
}
}
return tags.FlattenAndSet(d, model.Tags)
}
return nil
}

func resourceCustomProviderDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).CustomProviders.CustomProviderClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ResourceProviderID(d.Id())
id, err := customresourceprovider.ParseResourceProviderID(d.Id())
if err != nil {
return err
}

future, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("deleting Custom Provider %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of Custom Provider %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
if err := client.DeleteThenPoll(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}

return nil
}

func expandCustomProviderResourceType(input []interface{}) *[]customproviders.CustomRPResourceTypeRouteDefinition {
func expandCustomProviderResourceType(input []interface{}) *[]customresourceprovider.CustomRPResourceTypeRouteDefinition {
if len(input) == 0 {
return nil
}
definitions := make([]customproviders.CustomRPResourceTypeRouteDefinition, 0)
definitions := make([]customresourceprovider.CustomRPResourceTypeRouteDefinition, 0)

for _, v := range input {
if v == nil {
continue
}

attrs := v.(map[string]interface{})
definitions = append(definitions, customproviders.CustomRPResourceTypeRouteDefinition{
RoutingType: customproviders.ResourceTypeRouting(attrs["routing_type"].(string)),
Name: utils.String(attrs["name"].(string)),
Endpoint: utils.String(attrs["endpoint"].(string)),
definitions = append(definitions, customresourceprovider.CustomRPResourceTypeRouteDefinition{
RoutingType: utils.ToPtr(customresourceprovider.ResourceTypeRouting(attrs["routing_type"].(string))),
Name: attrs["name"].(string),
Endpoint: attrs["endpoint"].(string),
})
}

return &definitions
}

func flattenCustomProviderResourceType(input *[]customproviders.CustomRPResourceTypeRouteDefinition) []interface{} {
func flattenCustomProviderResourceType(input *[]customresourceprovider.CustomRPResourceTypeRouteDefinition) []interface{} {
if input == nil {
return []interface{}{}
}
Expand All @@ -255,43 +249,37 @@ func flattenCustomProviderResourceType(input *[]customproviders.CustomRPResource
for _, v := range *input {
definition := make(map[string]interface{})

definition["routing_type"] = string(v.RoutingType)

if v.Name != nil {
definition["name"] = *v.Name
}

if v.Endpoint != nil {
definition["endpoint"] = *v.Endpoint
}
definition["routing_type"] = v.RoutingType
definition["name"] = v.Name
definition["endpoint"] = v.Endpoint

definitions = append(definitions, definition)
}
return definitions
}

func expandCustomProviderAction(input []interface{}) *[]customproviders.CustomRPActionRouteDefinition {
func expandCustomProviderAction(input []interface{}) *[]customresourceprovider.CustomRPActionRouteDefinition {
if len(input) == 0 {
return nil
}
definitions := make([]customproviders.CustomRPActionRouteDefinition, 0)
definitions := make([]customresourceprovider.CustomRPActionRouteDefinition, 0)

for _, v := range input {
if v == nil {
continue
}

attrs := v.(map[string]interface{})
definitions = append(definitions, customproviders.CustomRPActionRouteDefinition{
Name: utils.String(attrs["name"].(string)),
Endpoint: utils.String(attrs["endpoint"].(string)),
definitions = append(definitions, customresourceprovider.CustomRPActionRouteDefinition{
Name: attrs["name"].(string),
Endpoint: attrs["endpoint"].(string),
})
}

return &definitions
}

func flattenCustomProviderAction(input *[]customproviders.CustomRPActionRouteDefinition) []interface{} {
func flattenCustomProviderAction(input *[]customresourceprovider.CustomRPActionRouteDefinition) []interface{} {
if input == nil {
return []interface{}{}
}
Expand All @@ -300,41 +288,36 @@ func flattenCustomProviderAction(input *[]customproviders.CustomRPActionRouteDef
for _, v := range *input {
definition := make(map[string]interface{})

if v.Name != nil {
definition["name"] = *v.Name
}

if v.Endpoint != nil {
definition["endpoint"] = *v.Endpoint
}
definition["name"] = v.Name
definition["endpoint"] = v.Endpoint

definitions = append(definitions, definition)
}
return definitions
}

func expandCustomProviderValidation(input []interface{}) *[]customproviders.CustomRPValidations {
func expandCustomProviderValidation(input []interface{}) *[]customresourceprovider.CustomRPValidations {
if len(input) == 0 {
return nil
}

validations := make([]customproviders.CustomRPValidations, 0)
validations := make([]customresourceprovider.CustomRPValidations, 0)

for _, v := range input {
if v == nil {
continue
}

attrs := v.(map[string]interface{})
validations = append(validations, customproviders.CustomRPValidations{
Specification: utils.String(attrs["specification"].(string)),
validations = append(validations, customresourceprovider.CustomRPValidations{
Specification: attrs["specification"].(string),
})
}

return &validations
}

func flattenCustomProviderValidation(input *[]customproviders.CustomRPValidations) []interface{} {
func flattenCustomProviderValidation(input *[]customresourceprovider.CustomRPValidations) []interface{} {
if input == nil {
return []interface{}{}
}
Expand All @@ -343,9 +326,7 @@ func flattenCustomProviderValidation(input *[]customproviders.CustomRPValidation
for _, v := range *input {
validation := make(map[string]interface{})

if v.Specification != nil {
validation["specification"] = *v.Specification
}
validation["specification"] = v.Specification

validations = append(validations, validation)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/customproviders/2018-09-01-preview/customresourceprovider"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/customproviders/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down Expand Up @@ -89,19 +90,19 @@ func TestAccCustomProvider_action(t *testing.T) {
}

func (r CustomProviderResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.ResourceProviderID(state.ID)
id, err := customresourceprovider.ParseResourceProviderID(state.ID)
if err != nil {
return nil, err
}
resp, err := client.CustomProviders.CustomProviderClient.Get(ctx, id.ResourceGroup, id.Name)
resp, err := client.CustomProviders.CustomProviderClient.Get(ctx, *id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
return utils.Bool(false), nil
}
return nil, fmt.Errorf("retrieving Custom Provider %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
return nil, fmt.Errorf("retrieving %s: %+v", *id, err)
}

return utils.Bool(true), nil
return utils.Bool(resp.Model != nil), nil
}

func (r CustomProviderResource) basic(data acceptance.TestData) string {
Expand Down
Loading