Skip to content

Commit

Permalink
azurerm_chaos_studio_target - add create method for resource (#24580)
Browse files Browse the repository at this point in the history
* add files for chaos studio targets

* go mod vendor

* fix lint errors

* go mod vendor

* goimports

* review comments
  • Loading branch information
stephybun authored Jan 23, 2024
1 parent 50058c5 commit 39ff4d7
Show file tree
Hide file tree
Showing 87 changed files with 4,245 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/clients/client_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,25 @@ import (
"fmt"

"github.com/hashicorp/terraform-provider-azurerm/internal/common"
chaosstudio "github.com/hashicorp/terraform-provider-azurerm/internal/services/chaosstudio/client"
containers "github.com/hashicorp/terraform-provider-azurerm/internal/services/containers/client"
devcenter "github.com/hashicorp/terraform-provider-azurerm/internal/services/devcenter/client"
loadtestservice "github.com/hashicorp/terraform-provider-azurerm/internal/services/loadtestservice/client"
managedidentity "github.com/hashicorp/terraform-provider-azurerm/internal/services/managedidentity/client"
)

type autoClient struct {
ChaosStudio *chaosstudio.AutoClient
ContainerService *containers.AutoClient
DevCenter *devcenter.AutoClient
LoadTestService *loadtestservice.AutoClient
ManagedIdentity *managedidentity.AutoClient
}

func buildAutoClients(client *autoClient, o *common.ClientOptions) (err error) {
if client.ChaosStudio, err = chaosstudio.NewClient(o); err != nil {
return fmt.Errorf("building client for ChaosStudio: %+v", err)
}

if client.ContainerService, err = containers.NewClient(o); err != nil {
return fmt.Errorf("building client for ContainerService: %+v", err)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package chaosstudio

import (
"context"
"fmt"
"strings"
"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/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/chaosstudio/2023-11-01/targets"
"github.com/hashicorp/go-azure-sdk/resource-manager/chaosstudio/2023-11-01/targettypes"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
)

func (r ChaosStudioTargetResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.ChaosStudio.V20231101.Targets
targetTypesClient := metadata.Client.ChaosStudio.V20231101.TargetTypes
subscriptionId := metadata.Client.Account.SubscriptionId

var config ChaosStudioTargetResourceSchema

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

locationId := targettypes.NewLocationID(subscriptionId, config.Location)
resp, err := targetTypesClient.ListComplete(ctx, locationId, targettypes.DefaultListOperationOptions())
if err != nil {
return fmt.Errorf("retrieving list of chaos target types: %+v", err)
}

// Validate name which needs to be of format [publisher]-[targetType]
// Only certain target types are accepted, and they could vary by region
targetTypes := map[string][]string{}
nameIsValid := false
for _, item := range resp.Items {
if name := item.Name; name != nil {
if strings.EqualFold(config.TargetType, *item.Name) {
nameIsValid = true
}
targetTypes[*item.Name] = pointer.From(item.Properties.ResourceTypes)
}
}

if !nameIsValid {
return fmt.Errorf("%q is not a valid `target_type` for the region %s, must be one of %+v", config.TargetType, config.Location, targetTypes)
}

id := commonids.NewChaosStudioTargetID(config.TargetResourceId, config.TargetType)

existing, err := client.Get(ctx, id)
if err != nil {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for the presence of an existing %s: %+v", id, err)
}
}
if !response.WasNotFound(existing.HttpResponse) {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

var payload targets.Target

// The API only accepts requests with an empty body for Properties
props := struct{}{}

payload.Location = pointer.To(location.Normalize(config.Location))
payload.Properties = pointer.To(props)

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

metadata.SetID(id)
return nil
},
Timeout: 30 * time.Minute,
}
}
18 changes: 18 additions & 0 deletions internal/services/chaosstudio/chaos_studio_target_resource_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package chaosstudio

// NOTE: this placeholder below is just to keep the compiler happy until the upstream
// PR https://github.com/hashicorp/pandora/pull/3671 is merged

var _ = ChaosStudioTargetResource{} // to keep the unused linter happy

type ChaosStudioTargetResource struct{}

type ChaosStudioTargetResourceSchema struct {
Location string `tfschema:"location"`
TargetType string `tfschema:"target_type"`
TargetResourceId string `tfschema:"target_resource_id"`
}

func (r ChaosStudioTargetResource) ResourceType() string {
return ""
}
27 changes: 27 additions & 0 deletions internal/services/chaosstudio/client/client_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package client

import (
"fmt"

chaosstudioV20231101 "github.com/hashicorp/go-azure-sdk/resource-manager/chaosstudio/2023-11-01"
"github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type AutoClient struct {
V20231101 chaosstudioV20231101.Client
}

func NewClient(o *common.ClientOptions) (*AutoClient, error) {

v20231101Client, err := chaosstudioV20231101.NewClientWithBaseURI(o.Environment.ResourceManager, func(c *resourcemanager.Client) {
o.Configure(c, o.Authorizers.ResourceManager)
})
if err != nil {
return nil, fmt.Errorf("building client for chaosstudio V20231101: %+v", err)
}

return &AutoClient{
V20231101: *v20231101Client,
}, nil
}
33 changes: 33 additions & 0 deletions internal/services/chaosstudio/registration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package chaosstudio

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

type Registration struct {
autoRegistration
}

// Name is the name of this Service
func (r Registration) Name() string {
return r.autoRegistration.Name()
}

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

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

// Resources returns a list of Resources supported by this Service
func (r Registration) Resources() []sdk.Resource {
resources := []sdk.Resource{}
resources = append(resources, r.autoRegistration.Resources()...)
return resources
}
26 changes: 26 additions & 0 deletions internal/services/chaosstudio/registration_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package chaosstudio

// NOTE: this file is generated - manual changes will be overwritten.

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

var _ sdk.TypedServiceRegistration = autoRegistration{}

type autoRegistration struct {
}

func (autoRegistration) Name() string {
return "ChaosStudio"
}

func (autoRegistration) DataSources() []sdk.DataSource {
return []sdk.DataSource{}
}

func (autoRegistration) Resources() []sdk.Resource {
return []sdk.Resource{}
}

func (autoRegistration) WebsiteCategories() []string {
return []string{}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 39ff4d7

Please sign in to comment.