Skip to content

Commit

Permalink
azurerm_trusted_signing_account - new resource support (#27720)
Browse files Browse the repository at this point in the history
* `azurerm_trusted_signing_account` - new resource support

* make generate

* update per comments

* update per comments

* update code

* update typo and code pattern

* normalize location
  • Loading branch information
ziyeqf authored Oct 31, 2024
1 parent 3dd484f commit 2d8b22f
Show file tree
Hide file tree
Showing 51 changed files with 2,802 additions and 0 deletions.
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/trustedsigning:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_trusted_signing_account((.|\n)*)###'

service/video-indexer:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_video_indexer_account((.|\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/trustedsigning:
- changed-files:
- any-glob-to-any-file:
- internal/services/codesigning/**/*

service/video-indexer:
- 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 @@ -124,6 +124,7 @@ var services = mapOf(
"synapse" to "Synapse",
"systemcentervirtualmachinemanager" to "System Center Virtual Machine Manager",
"trafficmanager" to "Traffic Manager",
"codesigning" to "Trusted Signing",
"vmware" to "VMware",
"videoindexer" to "VideoIndexer",
"voiceservices" to "Voice Services",
Expand Down
5 changes: 5 additions & 0 deletions internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
blueprints "github.com/hashicorp/terraform-provider-azurerm/internal/services/blueprints/client"
bot "github.com/hashicorp/terraform-provider-azurerm/internal/services/bot/client"
cdn "github.com/hashicorp/terraform-provider-azurerm/internal/services/cdn/client"
codesigning "github.com/hashicorp/terraform-provider-azurerm/internal/services/codesigning/client"
cognitiveServices "github.com/hashicorp/terraform-provider-azurerm/internal/services/cognitive/client"
communication "github.com/hashicorp/terraform-provider-azurerm/internal/services/communication/client"
compute "github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/client"
Expand Down Expand Up @@ -175,6 +176,7 @@ type Client struct {
Blueprints *blueprints.Client
Bot *bot.Client
Cdn *cdn.Client
CodeSigning *codesigning.Client
Cognitive *cognitiveServices.Client
Communication *communication.Client
Compute *compute.Client
Expand Down Expand Up @@ -349,6 +351,9 @@ func (client *Client) Build(ctx context.Context, o *common.ClientOptions) error
return fmt.Errorf("building clients for Bot: %+v", err)
}
client.Cdn = cdn.NewClient(o)
if client.CodeSigning, err = codesigning.NewClient(o); err != nil {
return fmt.Errorf("building clients for Code Signing: %+v", err)
}
if client.Cognitive, err = cognitiveServices.NewClient(o); err != nil {
return fmt.Errorf("building clients for Cognitive: %+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 @@ -24,6 +24,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/services/blueprints"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/bot"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/cdn"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/codesigning"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/cognitive"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/communication"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute"
Expand Down Expand Up @@ -149,6 +150,7 @@ func SupportedTypedServices() []sdk.TypedServiceRegistration {
azurestackhci.Registration{},
batch.Registration{},
bot.Registration{},
codesigning.Registration{},
cognitive.Registration{},
communication.Registration{},
compute.Registration{},
Expand Down
27 changes: 27 additions & 0 deletions internal/services/codesigning/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package client

import (
"fmt"

codesigning_v2024_09_30_preview "github.com/hashicorp/go-azure-sdk/resource-manager/codesigning/2024-09-30-preview"

"github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
Client codesigning_v2024_09_30_preview.Client
}

func NewClient(o *common.ClientOptions) (*Client, error) {
V20240930previewClient, err := codesigning_v2024_09_30_preview.NewClientWithBaseURI(o.Environment.ResourceManager, func(c *resourcemanager.Client) {
o.Configure(c, o.Authorizers.ResourceManager)
})
if err != nil {
return nil, fmt.Errorf("building client for codesigning v20240930preview: %+v", err)
}

return &Client{
Client: *V20240930previewClient,
}, nil
}
40 changes: 40 additions & 0 deletions internal/services/codesigning/registration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package codesigning

import (
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
)

type Registration struct{}

var _ sdk.TypedServiceRegistration = Registration{}

func (r Registration) AssociatedGitHubLabel() string {
return "service/trustedsigning"
}

// Name is the name of this Service
func (r Registration) Name() string {
return "Trusted Signing"
}

// WebsiteCategories returns a list of categories which can be used for the sidebar
func (r Registration) WebsiteCategories() []string {
return []string{
"Trusted Signing",
}
}

// DataSources returns a list of Data Sources supported by this Service
func (r Registration) DataSources() []sdk.DataSource {
return []sdk.DataSource{}
}

// Resources returns a list of Resources supported by this Service
func (r Registration) Resources() []sdk.Resource {
return []sdk.Resource{
TrustedSigningAccountResource{},
}
}
222 changes: 222 additions & 0 deletions internal/services/codesigning/trusted_signing_account_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package codesigning

import (
"context"
"fmt"
"regexp"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"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-sdk/resource-manager/codesigning/2024-09-30-preview/codesigningaccounts"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)

type TrustedSigningAccountModel struct {
Name string `tfschema:"name"`
Location string `tfschema:"location"`
ResourceGroupName string `tfschema:"resource_group_name"`
AccountUri string `tfschema:"account_uri"`
SkuName string `tfschema:"sku_name"`
Tags map[string]string `tfschema:"tags"`
}

type TrustedSigningAccountResource struct{}

var _ sdk.Resource = (*TrustedSigningAccountResource)(nil)

func (m TrustedSigningAccountResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.All(
validation.StringLenBetween(3, 24),
validation.StringMatch(
regexp.MustCompile("^[A-Za-z][A-Za-z0-9]*(?:-[A-Za-z0-9]+)*$"),
"An account's name must be between 3-24 alphanumeric characters. The name must begin with a letter, end with a letter or digit, and not contain consecutive hyphens.",
),
),
},

"location": commonschema.Location(),

"resource_group_name": commonschema.ResourceGroupName(),

"sku_name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(
codesigningaccounts.PossibleValuesForSkuName(),
false),
},

"tags": commonschema.Tags(),
}
}

func (m TrustedSigningAccountResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"account_uri": {
Type: pluginsdk.TypeString,
Computed: true,
},
}
}

func (m TrustedSigningAccountResource) ModelObject() interface{} {
return &TrustedSigningAccountModel{}
}

func (m TrustedSigningAccountResource) ResourceType() string {
return "azurerm_trusted_signing_account"
}

func (m TrustedSigningAccountResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, meta sdk.ResourceMetaData) error {
client := meta.Client.CodeSigning.Client.CodeSigningAccounts

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

subscriptionID := meta.Client.Account.SubscriptionId
id := codesigningaccounts.NewCodeSigningAccountID(subscriptionID, model.ResourceGroupName, model.Name)
existing, err := client.Get(ctx, id)
if !response.WasNotFound(existing.HttpResponse) {
if err != nil {
return fmt.Errorf("retrieving %s: %v", id, err)
}
return meta.ResourceRequiresImport(m.ResourceType(), id)
}

req := codesigningaccounts.CodeSigningAccount{
Name: &model.Name,
Location: location.Normalize(model.Location),
Tags: &model.Tags,
Properties: &codesigningaccounts.CodeSigningAccountProperties{
Sku: &codesigningaccounts.AccountSku{
Name: codesigningaccounts.SkuName(model.SkuName),
},
},
}

err = client.CreateThenPoll(ctx, id, req)
if err != nil {
return fmt.Errorf("creating %s: %v", id, err)
}

meta.SetID(id)
return nil
},
}
}

func (m TrustedSigningAccountResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, meta sdk.ResourceMetaData) error {
client := meta.Client.CodeSigning.Client.CodeSigningAccounts

id, err := codesigningaccounts.ParseCodeSigningAccountID(meta.ResourceData.Id())
if err != nil {
return err
}

result, err := client.Get(ctx, *id)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", id, err)
}

output := TrustedSigningAccountModel{
Name: id.CodeSigningAccountName,
ResourceGroupName: id.ResourceGroupName,
}

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

if prop := model.Properties; prop != nil {
output.AccountUri = pointer.From(prop.AccountUri)
if sku := prop.Sku; sku != nil {
output.SkuName = string(sku.Name)
}
}
}

return meta.Encode(&output)
},
}
}

func (m TrustedSigningAccountResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 10 * time.Minute,
Func: func(ctx context.Context, meta sdk.ResourceMetaData) (err error) {
client := meta.Client.CodeSigning.Client.CodeSigningAccounts
id, err := codesigningaccounts.ParseCodeSigningAccountID(meta.ResourceData.Id())
if err != nil {
return err
}
var model TrustedSigningAccountModel
if err = meta.Decode(&model); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

var patch codesigningaccounts.CodeSigningAccountPatch
if meta.ResourceData.HasChange("sku_name") {
patch.Properties = pointer.To(codesigningaccounts.CodeSigningAccountPatchProperties{
Sku: pointer.To(codesigningaccounts.AccountSkuPatch{
Name: pointer.To(codesigningaccounts.SkuName(model.SkuName)),
}),
})
}
if meta.ResourceData.HasChange("tags") {
patch.Tags = pointer.To(model.Tags)
}

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

return nil
},
}
}

func (m TrustedSigningAccountResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 10 * time.Minute,
Func: func(ctx context.Context, meta sdk.ResourceMetaData) error {
client := meta.Client.CodeSigning.Client.CodeSigningAccounts

id, err := codesigningaccounts.ParseCodeSigningAccountID(meta.ResourceData.Id())
if err != nil {
return err
}

meta.Logger.Infof("deleting %s", id)

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

func (m TrustedSigningAccountResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return codesigningaccounts.ValidateCodeSigningAccountID
}
Loading

0 comments on commit 2d8b22f

Please sign in to comment.