|
| 1 | +package network |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/hashicorp/go-azure-helpers/framework/typehelpers" |
| 9 | + "github.com/hashicorp/go-azure-helpers/lang/pointer" |
| 10 | + "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" |
| 11 | + "github.com/hashicorp/go-azure-helpers/resourcemanager/resourcegroups" |
| 12 | + "github.com/hashicorp/go-azure-sdk/resource-manager/network/2025-01-01/networksecuritygroups" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/list" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/list/schema" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 19 | + "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" |
| 20 | + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" |
| 21 | +) |
| 22 | + |
| 23 | +type NetworkSecurityGroupListResource struct { |
| 24 | + sdk.ListResourceMetadata |
| 25 | +} |
| 26 | + |
| 27 | +type NetworkSecurityGroupListModel struct { |
| 28 | + ResourceGroupName types.String `tfsdk:"resource_group_name"` |
| 29 | + SubscriptionId types.String `tfsdk:"subscription_id"` |
| 30 | +} |
| 31 | + |
| 32 | +var _ sdk.ListResourceWithRawV5Schemas = new(NetworkSecurityGroupListResource) |
| 33 | + |
| 34 | +func NewNetworkSecurityGroupListResource() list.ListResource { |
| 35 | + return new(NetworkSecurityGroupListResource) |
| 36 | +} |
| 37 | + |
| 38 | +func (r *NetworkSecurityGroupListResource) Metadata(_ context.Context, _ resource.MetadataRequest, response *resource.MetadataResponse) { |
| 39 | + response.TypeName = networkSecurityGroupResourceName |
| 40 | +} |
| 41 | + |
| 42 | +func (r *NetworkSecurityGroupListResource) ListResourceConfigSchema(_ context.Context, _ list.ListResourceSchemaRequest, response *list.ListResourceSchemaResponse) { |
| 43 | + response.Schema = schema.Schema{ |
| 44 | + Attributes: map[string]schema.Attribute{ |
| 45 | + "resource_group_name": schema.StringAttribute{ |
| 46 | + Optional: true, |
| 47 | + Validators: []validator.String{ |
| 48 | + typehelpers.WrappedStringValidator{ |
| 49 | + Func: resourcegroups.ValidateName, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + "subscription_id": schema.StringAttribute{ |
| 54 | + Optional: true, |
| 55 | + Validators: []validator.String{ |
| 56 | + typehelpers.WrappedStringValidator{ |
| 57 | + Func: validation.IsUUID, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func (r *NetworkSecurityGroupListResource) List(ctx context.Context, request list.ListRequest, stream *list.ListResultsStream) { |
| 66 | + client := r.Client.Network.NetworkSecurityGroups |
| 67 | + |
| 68 | + ctx, cancel := context.WithTimeout(ctx, time.Minute*60) |
| 69 | + defer cancel() |
| 70 | + |
| 71 | + var data NetworkSecurityGroupListModel |
| 72 | + diags := request.Config.Get(ctx, &data) |
| 73 | + if diags.HasError() { |
| 74 | + stream.Results = list.ListResultsStreamDiagnostics(diags) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + results := make([]networksecuritygroups.NetworkSecurityGroup, 0) |
| 79 | + |
| 80 | + subscriptionID := r.SubscriptionId |
| 81 | + if !data.SubscriptionId.IsNull() { |
| 82 | + subscriptionID = data.SubscriptionId.ValueString() |
| 83 | + } |
| 84 | + |
| 85 | + switch { |
| 86 | + case !data.ResourceGroupName.IsNull(): |
| 87 | + resp, err := client.ListComplete(ctx, commonids.NewResourceGroupID(subscriptionID, data.ResourceGroupName.ValueString())) |
| 88 | + if err != nil { |
| 89 | + sdk.SetResponseErrorDiagnostic(stream, fmt.Sprintf("listing `%s`", networkSecurityGroupResourceName), err) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + results = resp.Items |
| 94 | + default: |
| 95 | + resp, err := client.ListAllComplete(ctx, commonids.NewSubscriptionID(subscriptionID)) |
| 96 | + if err != nil { |
| 97 | + sdk.SetResponseErrorDiagnostic(stream, fmt.Sprintf("listing `%s`", networkSecurityGroupResourceName), err) |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + results = resp.Items |
| 102 | + } |
| 103 | + |
| 104 | + stream.Results = func(push func(list.ListResult) bool) { |
| 105 | + for _, nsg := range results { |
| 106 | + result := request.NewListResult(ctx) |
| 107 | + result.DisplayName = pointer.From(nsg.Name) |
| 108 | + |
| 109 | + id, err := networksecuritygroups.ParseNetworkSecurityGroupID(pointer.From(nsg.Id)) |
| 110 | + if err != nil { |
| 111 | + sdk.SetResponseErrorDiagnostic(stream, "parsing Network Security Group ID", err) |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + rd := resourceNetworkSecurityGroup().Data(&terraform.InstanceState{}) |
| 116 | + rd.SetId(id.ID()) |
| 117 | + |
| 118 | + if err := resourceNetworkSecurityGroupFlatten(rd, id, &nsg); err != nil { |
| 119 | + sdk.SetResponseErrorDiagnostic(stream, fmt.Sprintf("encoding `%s` resource data", networkSecurityGroupResourceName), err) |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + tfTypeIdentity, err := rd.TfTypeIdentityState() |
| 124 | + if err != nil { |
| 125 | + sdk.SetResponseErrorDiagnostic(stream, "converting Identity State", err) |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + if err := result.Identity.Set(ctx, *tfTypeIdentity); err != nil { |
| 130 | + sdk.SetResponseErrorDiagnostic(stream, "setting Identity Data", err) |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + tfTypeResourceState, err := rd.TfTypeResourceState() |
| 135 | + if err != nil { |
| 136 | + sdk.SetResponseErrorDiagnostic(stream, "converting Resource State", err) |
| 137 | + return |
| 138 | + } |
| 139 | + |
| 140 | + if err := result.Resource.Set(ctx, *tfTypeResourceState); err != nil { |
| 141 | + sdk.SetResponseErrorDiagnostic(stream, "setting Resource Data", err) |
| 142 | + return |
| 143 | + } |
| 144 | + |
| 145 | + if !push(result) { |
| 146 | + return |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func (r *NetworkSecurityGroupListResource) Configure(_ context.Context, request resource.ConfigureRequest, response *resource.ConfigureResponse) { |
| 153 | + r.Defaults(request, response) |
| 154 | +} |
| 155 | + |
| 156 | +func (r *NetworkSecurityGroupListResource) RawV5Schemas(ctx context.Context, _ list.RawV5SchemaRequest, response *list.RawV5SchemaResponse) { |
| 157 | + res := resourceNetworkSecurityGroup() |
| 158 | + response.ProtoV5Schema = res.ProtoSchema(ctx)() |
| 159 | + response.ProtoV5IdentitySchema = res.ProtoIdentitySchema(ctx)() |
| 160 | +} |
0 commit comments