-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
azurerm_chaos_studio_target
- add create method for resource (#24580)
* add files for chaos studio targets * go mod vendor * fix lint errors * go mod vendor * goimports * review comments
- Loading branch information
Showing
87 changed files
with
4,245 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
internal/services/chaosstudio/chaos_studio_target_resource_create.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
internal/services/chaosstudio/chaos_studio_target_resource_gen.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | ||
} |
106 changes: 106 additions & 0 deletions
106
...orp/go-azure-sdk/resource-manager/chaosstudio/2023-11-01/capabilities/README.md
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
...com/hashicorp/go-azure-sdk/resource-manager/chaosstudio/2023-11-01/capabilities/client.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.