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

network - update several resources to use hashicorp/go-azure-sdk #25905

Merged
merged 10 commits into from
May 14, 2024
333 changes: 168 additions & 165 deletions internal/services/network/custom_ip_prefix_resource.go

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions internal/services/network/custom_ip_prefix_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-09-01/customipprefixes"
"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/network/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type CustomIpPrefixResource struct{}
Expand Down Expand Up @@ -125,21 +126,21 @@ func testAccCustomIpPrefix_ipv6(t *testing.T) {
}

func (CustomIpPrefixResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.CustomIpPrefixID(state.ID)
id, err := customipprefixes.ParseCustomIPPrefixID(state.ID)
if err != nil {
return nil, err
}

resp, err := client.Network.CustomIPPrefixesClient.Get(ctx, id.ResourceGroup, id.Name, "")
resp, err := client.Network.Client.CustomIPPrefixes.Get(ctx, *id, customipprefixes.DefaultGetOperationOptions())
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return utils.Bool(false), nil
if response.WasNotFound(resp.HttpResponse) {
return pointer.To(false), nil
}

return nil, fmt.Errorf("retrieving %s: %+v", *id, err)
}

return utils.Bool(true), nil
return pointer.To(true), nil
}

func (r CustomIpPrefixResource) ipv4Provisioned(data acceptance.TestData) string {
Expand Down
116 changes: 64 additions & 52 deletions internal/services/network/ip_group_cidr_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-09-01/ipgroups"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
Expand All @@ -17,12 +19,11 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
"github.com/tombuildsstuff/kermit/sdk/network/2022-07-01/network"
)

func resourceIpGroupCidr() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceIpGroupCidrCreateUpdate,
Create: resourceIpGroupCidrCreate,
Read: resourceIpGroupCidrRead,
Delete: resourceIpGroupCidrDelete,

Expand Down Expand Up @@ -54,58 +55,58 @@ func resourceIpGroupCidr() *pluginsdk.Resource {
}
}

func resourceIpGroupCidrCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.IPGroupsClient
func resourceIpGroupCidrCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.Client.IPGroups
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

cidr := d.Get("cidr").(string)
cidrName := strings.ReplaceAll(cidr, "/", "_")
ipGroupId, err := parse.IpGroupID(d.Get("ip_group_id").(string))
ipGroupId, err := ipgroups.ParseIPGroupID(d.Get("ip_group_id").(string))
if err != nil {
return err
}
id := parse.NewIpGroupCidrID(subscriptionId, ipGroupId.ResourceGroup, ipGroupId.Name, cidrName)
id := parse.NewIpGroupCidrID(subscriptionId, ipGroupId.ResourceGroupName, ipGroupId.IpGroupName, cidrName)

locks.ByID(ipGroupId.ID())
defer locks.UnlockByID(ipGroupId.ID())

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

if d.IsNewResource() {
if utils.SliceContainsValue(*existing.IPAddresses, cidr) {
return tf.ImportAsExistsError("azurerm_ip_group_cidr", id.ID())
}
if existing.Model == nil {
return fmt.Errorf("retrieving %s: `model` was nil", ipGroupId)
}
if existing.Model.Properties == nil {
return fmt.Errorf("retrieving %s: `properties` was nil", ipGroupId)
}

if utils.SliceContainsValue(*existing.Model.Properties.IPAddresses, cidr) {
return tf.ImportAsExistsError("azurerm_ip_group_cidr", id.ID())
}

ipAddresses := make([]string, 0)
if existing.IPAddresses != nil {
ipAddresses = *existing.IPAddresses
if existing.Model.Properties.IPAddresses != nil {
ipAddresses = *existing.Model.Properties.IPAddresses
}
ipAddresses = append(ipAddresses, cidr)

params := network.IPGroup{
Name: &ipGroupId.Name,
Location: existing.Location,
Tags: existing.Tags,
IPGroupPropertiesFormat: &network.IPGroupPropertiesFormat{
params := ipgroups.IPGroup{
Name: &ipGroupId.IpGroupName,
Location: existing.Model.Location,
Tags: existing.Model.Tags,
Properties: &ipgroups.IPGroupPropertiesFormat{
IPAddresses: &ipAddresses,
},
}

future, err := client.CreateOrUpdate(ctx, ipGroupId.ResourceGroup, ipGroupId.Name, params)
if 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 the completion of %s: %+v", id, err)
if err := client.CreateOrUpdateThenPoll(ctx, *ipGroupId, params); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

d.SetId(id.ID())
Expand All @@ -114,23 +115,29 @@ func resourceIpGroupCidrCreateUpdate(d *pluginsdk.ResourceData, meta interface{}
}

func resourceIpGroupCidrRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.IPGroupsClient
client := meta.(*clients.Client).Network.Client.IPGroups
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.IpGroupCidrID(d.Id())
if err != nil {
return err
}
ipGroupId := parse.NewIpGroupID(id.SubscriptionId, id.ResourceGroup, id.IpGroupName)
ipGroupId := ipgroups.NewIPGroupID(id.SubscriptionId, id.ResourceGroup, id.IpGroupName)
cidr := strings.ReplaceAll(id.CidrName, "_", "/")

resp, err := client.Get(ctx, id.ResourceGroup, id.IpGroupName, "")
resp, err := client.Get(ctx, ipGroupId, ipgroups.DefaultGetOperationOptions())
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("making Read request on IP Group %q (Resource Group %q): %+v", ipGroupId.Name, ipGroupId.ResourceGroup, err)
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("retrieving %s: %+v", ipGroupId, err)
}
if resp.Model == nil {
return fmt.Errorf("retrieving %s: `model` was nil", ipGroupId)
}
if resp.Model.Properties == nil {
return fmt.Errorf("retrieving %s: `properties` was nil", ipGroupId)
}
if !utils.SliceContainsValue(*resp.IPAddresses, cidr) {
if !utils.SliceContainsValue(*resp.Model.Properties.IPAddresses, cidr) {
d.SetId("")
return nil
}
Expand All @@ -143,45 +150,50 @@ func resourceIpGroupCidrRead(d *pluginsdk.ResourceData, meta interface{}) error
}

func resourceIpGroupCidrDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.IPGroupsClient
client := meta.(*clients.Client).Network.Client.IPGroups
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

cidr := d.Get("cidr").(string)
ipGroupId, err := parse.IpGroupID(d.Get("ip_group_id").(string))
id, err := parse.IpGroupCidrID(d.Id())
if err != nil {
return err
}

// TODO this resource should use a composite resource ID to remove this instance of d.Get() in the Delete
// this file can then be removed from the exceptions list in the run-gradually-deprecated.sh script
cidr := d.Get("cidr").(string)
ipGroupId := ipgroups.NewIPGroupID(id.SubscriptionId, id.ResourceGroup, id.IpGroupName)

locks.ByID(ipGroupId.ID())
defer locks.UnlockByID(ipGroupId.ID())

existing, err := client.Get(ctx, ipGroupId.ResourceGroup, ipGroupId.Name, "")
existing, err := client.Get(ctx, ipGroupId, ipgroups.DefaultGetOperationOptions())
if err != nil {
if utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("reading existing %s: %s", ipGroupId, err)
if response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("retrieving %s: %s", ipGroupId, err)
}
}
if existing.Model == nil {
return fmt.Errorf("retrieving %s: `model` was nil", ipGroupId)
}
if existing.Model.Properties == nil {
return fmt.Errorf("retrieving %s: `properties` was nil", ipGroupId)
}

ipAddresses := *existing.IPAddresses
ipAddresses := *existing.Model.Properties.IPAddresses
ipAddresses = utils.RemoveFromStringArray(ipAddresses, cidr)

params := network.IPGroup{
Name: &ipGroupId.Name,
Location: existing.Location,
Tags: existing.Tags,
IPGroupPropertiesFormat: &network.IPGroupPropertiesFormat{
params := ipgroups.IPGroup{
Name: &ipGroupId.IpGroupName,
Location: existing.Model.Location,
Tags: existing.Model.Tags,
Properties: &ipgroups.IPGroupPropertiesFormat{
IPAddresses: &ipAddresses,
},
}

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

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("deleting IP Group CIDR %q (IP Group %q - Resource Group %q): %+v", cidr, ipGroupId.Name, ipGroupId.ResourceGroup, err)
if err := client.CreateOrUpdateThenPoll(ctx, ipGroupId, params); err != nil {
return fmt.Errorf("updating %s: %+v", ipGroupId.ID(), err)
}

return err
Expand Down
21 changes: 16 additions & 5 deletions internal/services/network/ip_group_cidr_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-09-01/ipgroups"
"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"
Expand Down Expand Up @@ -79,16 +81,25 @@ func (t IPGroupCidrResource) Exists(ctx context.Context, clients *clients.Client
return nil, err
}

resp, err := clients.Network.IPGroupsClient.Get(ctx, id.ResourceGroup, id.IpGroupName, "")
ipGroupId := ipgroups.NewIPGroupID(id.SubscriptionId, id.ResourceGroup, id.IpGroupName)

resp, err := clients.Network.Client.IPGroups.Get(ctx, ipGroupId, ipgroups.DefaultGetOperationOptions())
if err != nil {
return nil, fmt.Errorf("reading IP Group (%s): %+v", id, err)
return nil, fmt.Errorf("retrieving %s: %+v", id, err)
}

if resp.Model == nil {
return nil, fmt.Errorf("retrieving %s: `model` was nil", ipGroupId)
}
if resp.Model.Properties == nil {
return nil, fmt.Errorf("retrieving %s: `properties` was nil", ipGroupId)
}

if !utils.SliceContainsValue(*resp.IPAddresses, state.Attributes["cidr"]) {
return utils.Bool(false), nil
if !utils.SliceContainsValue(*resp.Model.Properties.IPAddresses, state.Attributes["cidr"]) {
return pointer.To(false), nil
}

return utils.Bool(true), nil
return pointer.To(true), nil
}

func (IPGroupCidrResource) basic(data acceptance.TestData) string {
Expand Down
48 changes: 23 additions & 25 deletions internal/services/network/ip_group_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-09-01/ipgroups"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceIpGroup() *pluginsdk.Resource {
Expand Down Expand Up @@ -42,45 +42,43 @@ func dataSourceIpGroup() *pluginsdk.Resource {
Set: pluginsdk.HashString,
},

"tags": tags.SchemaDataSource(),
"tags": commonschema.TagsDataSource(),
},
}
}

func dataSourceIpGroupRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.IPGroupsClient
client := meta.(*clients.Client).Network.Client.IPGroups
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewIpGroupID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
id := ipgroups.NewIPGroupID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

resp, err := client.Get(ctx, id.ResourceGroup, id.Name, "")
resp, err := client.Get(ctx, id, ipgroups.DefaultGetOperationOptions())
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("making Read request on %s: %+v", id, err)
return fmt.Errorf("retrieving %s: %+v", id, err)
}

if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("reading request on %s: %+v", id, err)
}
d.SetId(id.ID())

d.Set("name", resp.Name)
d.Set("resource_group_name", id.ResourceGroup)

d.Set("location", location.NormalizeNilable(resp.Location))

if props := resp.IPGroupPropertiesFormat; props != nil {
if props.IPAddresses == nil {
return fmt.Errorf("list of ipAddresses returned is nil")
}
if err := d.Set("cidrs", props.IPAddresses); err != nil {
return fmt.Errorf("setting `cidrs`: %+v", err)
d.Set("name", id.IpGroupName)
d.Set("resource_group_name", id.ResourceGroupName)

if model := resp.Model; model != nil {
d.Set("location", location.NormalizeNilable(model.Location))
if props := model.Properties; props != nil {
if props.IPAddresses == nil {
return fmt.Errorf("list of ipAddresses returned is nil")
}
if err := d.Set("cidrs", props.IPAddresses); err != nil {
return fmt.Errorf("setting `cidrs`: %+v", err)
}
}
return tags.FlattenAndSet(d, model.Tags)
}

return tags.FlattenAndSet(d, resp.Tags)
return nil
}
Loading
Loading