Skip to content

Commit

Permalink
add files for chaos studio targets
Browse files Browse the repository at this point in the history
  • Loading branch information
stephybun committed Jan 22, 2024
1 parent 1ed8f97 commit 29728cf
Show file tree
Hide file tree
Showing 89 changed files with 4,606 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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/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 := targets.NewScopedTargetID(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,
}
}
44 changes: 44 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,44 @@
package chaosstudio

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

var _ sdk.Resource = ChaosStudioTargetResource{}

type ChaosStudioTargetResource struct{}

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

func (r ChaosStudioTargetResource) ModelObject() interface{} {
return nil
}

func (r ChaosStudioTargetResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return nil
}

func (r ChaosStudioTargetResource) ResourceType() string {
return ""
}

func (r ChaosStudioTargetResource) Arguments() map[string]*pluginsdk.Schema {
return nil
}

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

func (r ChaosStudioTargetResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{}
}

func (r ChaosStudioTargetResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{}
}
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
}
30 changes: 30 additions & 0 deletions internal/services/chaosstudio/registration_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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{
ChaosStudioTargetResource{},
}
}

func (autoRegistration) WebsiteCategories() []string {
return []string{
"Chaos Studio",
}
}

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 29728cf

Please sign in to comment.