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

New Resource: azurerm_video_indexer_account #27632

Merged
merged 8 commits into from
Oct 23, 2024
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
3 changes: 3 additions & 0 deletions .github/labeler-issue-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ service/systemcentervirtualmachinemanager:
service/traffic-manager:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_traffic_manager_((.|\n)*)###'

service/video-indexer:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_video_indexer_account((.|\n)*)###'

service/virtual-desktops:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_virtual_desktop_((.|\n)*)###'

Expand Down
5 changes: 5 additions & 0 deletions .github/labeler-pull-request-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,11 @@ service/traffic-manager:
- any-glob-to-any-file:
- internal/services/trafficmanager/**/*

service/video-indexer:
- changed-files:
- any-glob-to-any-file:
- internal/services/videoindexer/**/*

service/virtual-desktops:
- changed-files:
- any-glob-to-any-file:
Expand Down
1 change: 1 addition & 0 deletions .teamcity/components/generated/services.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ var services = mapOf(
"systemcentervirtualmachinemanager" to "System Center Virtual Machine Manager",
"trafficmanager" to "Traffic Manager",
"vmware" to "VMware",
"videoindexer" to "VideoIndexer",
"voiceservices" to "Voice Services",
"web" to "Web",
"workloads" to "Workloads"
Expand Down
7 changes: 7 additions & 0 deletions internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ import (
synapse "github.com/hashicorp/terraform-provider-azurerm/internal/services/synapse/client"
systemCenterVirtualMachineManager "github.com/hashicorp/terraform-provider-azurerm/internal/services/systemcentervirtualmachinemanager/client"
trafficManager "github.com/hashicorp/terraform-provider-azurerm/internal/services/trafficmanager/client"
videoindexer "github.com/hashicorp/terraform-provider-azurerm/internal/services/videoindexer/client"
vmware "github.com/hashicorp/terraform-provider-azurerm/internal/services/vmware/client"
voiceServices "github.com/hashicorp/terraform-provider-azurerm/internal/services/voiceservices/client"
web "github.com/hashicorp/terraform-provider-azurerm/internal/services/web/client"
Expand Down Expand Up @@ -270,6 +271,7 @@ type Client struct {
Synapse *synapse.Client
SystemCenterVirtualMachineManager *systemcentervirtualmachinemanager_2023_10_07.Client
TrafficManager *trafficManager.Client
VideoIndexer *videoindexer.Client
Vmware *vmware.Client
VoiceServices *voiceServices.Client
Web *web.Client
Expand Down Expand Up @@ -632,6 +634,11 @@ func (client *Client) Build(ctx context.Context, o *common.ClientOptions) error
if client.TrafficManager, err = trafficManager.NewClient(o); err != nil {
return fmt.Errorf("building clients for Traffic Manager: %+v", err)
}

if client.VideoIndexer, err = videoindexer.NewClient(o); err != nil {
return fmt.Errorf("building clients for Video Indexer: %+v", err)
}

if client.Vmware, err = vmware.NewClient(o); err != nil {
return fmt.Errorf("building clients for VMWare: %+v", err)
}
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/services/synapse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/systemcentervirtualmachinemanager"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/trafficmanager"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/videoindexer"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/vmware"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/voiceservices"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/web"
Expand Down Expand Up @@ -213,6 +214,7 @@ func SupportedTypedServices() []sdk.TypedServiceRegistration {
streamanalytics.Registration{},
subscription.Registration{},
systemcentervirtualmachinemanager.Registration{},
videoindexer.Registration{},
vmware.Registration{},
voiceservices.Registration{},
web.Registration{},
Expand Down
300 changes: 300 additions & 0 deletions internal/services/videoindexer/account_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,300 @@
package videoindexer

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/videoindexer/2024-01-01/accounts"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appservice/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type AccountResource struct{}

var _ sdk.ResourceWithUpdate = AccountResource{}

type AccountModel struct {
Name string `tfschema:"name"`
ResourceGroup string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
Storage []StorageModel `tfschema:"storage"`
Identity []identity.ModelSystemAssignedUserAssigned `tfschema:"identity"`
Tags map[string]string `tfschema:"tags"`
}

type StorageModel struct {
StorageAccountId string `tfschema:"storage_account_id"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this just be named account_id since it's already under a storage block?

UserAssignedIdentityId string `tfschema:"user_assigned_identity_id"`
}

func (r AccountResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.ServicePlanName,
},

"resource_group_name": commonschema.ResourceGroupName(),

"location": commonschema.Location(),

"storage": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"storage_account_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: commonids.ValidateStorageAccountID,
},
"user_assigned_identity_id": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: commonids.ValidateUserAssignedIdentityID,
},
},
},
},

"identity": commonschema.SystemAssignedUserAssignedIdentityRequired(),

"tags": tags.Schema(),
}
}

func (r AccountResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{}
}

func (r AccountResource) ModelObject() interface{} {
return &AccountModel{}
}

func (r AccountResource) ResourceType() string {
return "azurerm_video_indexer_account"
}

func (r AccountResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return accounts.ValidateAccountID
}

func (r AccountResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 60 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var account AccountModel
if err := metadata.Decode(&account); err != nil {
return err
}

client := metadata.Client.VideoIndexer.AccountClient
subscriptionId := metadata.Client.Account.SubscriptionId

id := accounts.NewAccountID(subscriptionId, account.ResourceGroup, account.Name)

existing, err := client.Get(ctx, id)
if err != nil && !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("retrieving %s: %v", id, err)
}
if !response.WasNotFound(existing.HttpResponse) {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

expandedIdentity, err := identity.ExpandLegacySystemAndUserAssignedMapFromModel(account.Identity)
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
}

payload := accounts.Account{
Identity: expandedIdentity,
Location: location.Normalize(account.Location),
Tags: pointer.To(account.Tags),
Properties: &accounts.AccountPropertiesForPutRequest{
StorageServices: expandStorageForCreate(account.Storage),
},
}

if _, err := client.CreateOrUpdate(ctx, id, payload); err != nil {
return fmt.Errorf("creating %s: %v", id, err)
}

metadata.SetID(id)

return nil
},
}
}

func (r AccountResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.VideoIndexer.AccountClient
id, err := accounts.ParseAccountID(metadata.ResourceData.Id())
if err != nil {
return err
}

account, err := client.Get(ctx, *id)
if err != nil {
if response.WasNotFound(account.HttpResponse) {
return metadata.MarkAsGone(id)
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}

state := AccountModel{
Name: id.AccountName,
ResourceGroup: id.ResourceGroupName,
}

if model := account.Model; model != nil {
state.Location = location.Normalize(model.Location)
state.Tags = pointer.From(model.Tags)

flattenedIdentity, err := identity.FlattenLegacySystemAndUserAssignedMapToModel(model.Identity)
if err != nil {
return fmt.Errorf("flattening `identity`: %+v", err)
}
state.Identity = flattenedIdentity

if props := model.Properties; props != nil {
state.Storage, err = flattenStorage(props.StorageServices)
if err != nil {
return fmt.Errorf("flattening `storage`: %+v", err)
}
}
}

return metadata.Encode(&state)
},
}
}

func (r AccountResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 60 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
id, err := accounts.ParseAccountID(metadata.ResourceData.Id())
if err != nil {
return err
}

client := metadata.Client.VideoIndexer.AccountClient

var account AccountModel
if err := metadata.Decode(&account); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

payload := accounts.AccountPatch{}

if metadata.ResourceData.HasChange("tags") {
payload.Tags = pointer.To(account.Tags)
}

if metadata.ResourceData.HasChange("identity") {
expandedIdentity, err := identity.ExpandLegacySystemAndUserAssignedMapFromModel(account.Identity)
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
}
payload.Identity = expandedIdentity
}

if metadata.ResourceData.HasChange("storage") {
payload.Properties = &accounts.AccountPropertiesForPatchRequest{
StorageServices: expandStorageForUpdate(account.Storage),
}
}

if _, err := client.Update(ctx, *id, payload); err != nil {
return fmt.Errorf("updating %s: %v", id, err)
}

return nil
},
}
}

func (r AccountResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 60 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.VideoIndexer.AccountClient

id, err := accounts.ParseAccountID(metadata.ResourceData.Id())
metadata.Logger.Infof("deleting %s", id)

if err != nil {
return err
}

if _, err := client.Delete(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %v", id, err)
}

return nil
},
}
}

func expandStorageForCreate(input []StorageModel) *accounts.StorageServicesForPutRequest {
if len(input) == 0 {
return &accounts.StorageServicesForPutRequest{}
}

return &accounts.StorageServicesForPutRequest{
ResourceId: pointer.To(input[0].StorageAccountId),
UserAssignedIdentity: pointer.To(input[0].UserAssignedIdentityId),
}
}

func expandStorageForUpdate(input []StorageModel) *accounts.StorageServicesForPatchRequest {
if len(input) == 0 {
return &accounts.StorageServicesForPatchRequest{}
}

return &accounts.StorageServicesForPatchRequest{
UserAssignedIdentity: pointer.To(input[0].UserAssignedIdentityId),
}
}

func flattenStorage(input *accounts.StorageServicesForPutRequest) ([]StorageModel, error) {
if input == nil {
return []StorageModel{}, nil
}

storage := StorageModel{}
if v := pointer.From(input.ResourceId); v != "" {
id, err := commonids.ParseStorageAccountID(v)
if err != nil {
return []StorageModel{}, err
}
storage.StorageAccountId = id.ID()
}

if v := pointer.From(input.UserAssignedIdentity); v != "" {
id, err := commonids.ParseUserAssignedIdentityID(v)
if err != nil {
return []StorageModel{}, err
}
storage.UserAssignedIdentityId = id.ID()
}

return []StorageModel{storage}, nil
}
Loading
Loading