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_kubernetes_fleet_update_strategy #24328

Merged
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
6 changes: 6 additions & 0 deletions internal/services/containers/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/containerservice/2023-06-02-preview/maintenanceconfigurations"
"github.com/hashicorp/go-azure-sdk/resource-manager/containerservice/2023-06-02-preview/managedclusters"
"github.com/hashicorp/go-azure-sdk/resource-manager/containerservice/2023-06-02-preview/snapshots"
"github.com/hashicorp/go-azure-sdk/resource-manager/containerservice/2023-10-15/fleetupdatestrategies"
"github.com/hashicorp/go-azure-sdk/resource-manager/kubernetesconfiguration/2022-11-01/extensions"
"github.com/hashicorp/go-azure-sdk/resource-manager/kubernetesconfiguration/2022-11-01/fluxconfiguration"
"github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager"
Expand All @@ -27,6 +28,7 @@ type Client struct {
ContainerRegistryClient_v2021_08_01_preview *containerregistry_v2021_08_01_preview.Client
// v2019_06_01_preview is needed for container registry agent pools and tasks
ContainerRegistryClient_v2019_06_01_preview *containerregistry_v2019_06_01_preview.Client
FleetUpdateStrategiesClient *fleetupdatestrategies.FleetUpdateStrategiesClient
KubernetesClustersClient *managedclusters.ManagedClustersClient
KubernetesExtensionsClient *extensions.ExtensionsClient
KubernetesFluxConfigurationClient *fluxconfiguration.FluxConfigurationClient
Expand Down Expand Up @@ -55,6 +57,9 @@ func NewContainersClient(o *common.ClientOptions) (*Client, error) {
}

// AKS
fleetUpdateStrategiesClient := fleetupdatestrategies.NewFleetUpdateStrategiesClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&fleetUpdateStrategiesClient.Client, o.ResourceManagerAuthorizer)

kubernetesClustersClient := managedclusters.NewManagedClustersClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&kubernetesClustersClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -87,6 +92,7 @@ func NewContainersClient(o *common.ClientOptions) (*Client, error) {
ContainerInstanceClient: &containerInstanceClient,
ContainerRegistryClient_v2021_08_01_preview: containerRegistryClient_v2021_08_01_preview,
ContainerRegistryClient_v2019_06_01_preview: containerRegistryClient_v2019_06_01_preview,
FleetUpdateStrategiesClient: &fleetUpdateStrategiesClient,
KubernetesClustersClient: &kubernetesClustersClient,
KubernetesExtensionsClient: kubernetesExtensionsClient,
KubernetesFluxConfigurationClient: fluxConfigurationClient,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
package containers

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-sdk/resource-manager/containerservice/2023-10-15/fleetupdatestrategies"
"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"
)

var _ sdk.Resource = KubernetesFleetUpdateStrategyResource{}
var _ sdk.ResourceWithUpdate = KubernetesFleetUpdateStrategyResource{}

type KubernetesFleetUpdateStrategyResource struct{}

func (r KubernetesFleetUpdateStrategyResource) ModelObject() interface{} {
return &KubernetesFleetUpdateStrategyResourceSchema{}
}

type KubernetesFleetUpdateStrategyResourceSchema struct {
KubernetesFleetManagerId string `tfschema:"kubernetes_fleet_manager_id"`
Name string `tfschema:"name"`
Stage []KubernetesFleetUpdateStrategyResourceUpdateStageSchema `tfschema:"stage"`
}

type KubernetesFleetUpdateStrategyResourceUpdateGroupSchema struct {
Name string `tfschema:"name"`
}

type KubernetesFleetUpdateStrategyResourceUpdateStageSchema struct {
AfterStageWaitInSeconds int64 `tfschema:"after_stage_wait_in_seconds"`
Group []KubernetesFleetUpdateStrategyResourceUpdateGroupSchema `tfschema:"group"`
Name string `tfschema:"name"`
}

func (r KubernetesFleetUpdateStrategyResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return fleetupdatestrategies.ValidateUpdateStrategyID
}

func (r KubernetesFleetUpdateStrategyResource) ResourceType() string {
return "azurerm_kubernetes_fleet_update_strategy"
}

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

"kubernetes_fleet_manager_id": commonschema.ResourceIDReferenceRequiredForceNew(&fleetupdatestrategies.FleetId{}),

"stage": {
Required: true,
Type: pluginsdk.TypeList,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Required: true,
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},

"group": {
Required: true,
Type: pluginsdk.TypeList,
MinItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Required: true,
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
},

"after_stage_wait_in_seconds": {
Optional: true,
Type: pluginsdk.TypeInt,
},
},
},
},
}
}

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

func (r KubernetesFleetUpdateStrategyResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Containers.FleetUpdateStrategiesClient

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

fleetId, err := commonids.ParseFleetID(config.KubernetesFleetManagerId)
if err != nil {
return err
}

id := fleetupdatestrategies.NewUpdateStrategyID(fleetId.SubscriptionId, fleetId.ResourceGroupName, fleetId.FleetName, config.Name)

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)
}

payload := fleetupdatestrategies.FleetUpdateStrategy{
Properties: &fleetupdatestrategies.FleetUpdateStrategyProperties{
Strategy: fleetupdatestrategies.UpdateRunStrategy{
Stages: expandKubernetesFleetUpdateStrategyStage(config.Stage),
},
},
}

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

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

func (r KubernetesFleetUpdateStrategyResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Containers.FleetUpdateStrategiesClient

id, err := fleetupdatestrategies.ParseUpdateStrategyID(metadata.ResourceData.Id())
if err != nil {
return err
}

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

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

if metadata.ResourceData.HasChange("stage") {
payload.Properties.Strategy.Stages = expandKubernetesFleetUpdateStrategyStage(config.Stage)
}

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

return nil
},
}
}

func (r KubernetesFleetUpdateStrategyResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Containers.FleetUpdateStrategiesClient
schema := KubernetesFleetUpdateStrategyResourceSchema{}

id, err := fleetupdatestrategies.ParseUpdateStrategyID(metadata.ResourceData.Id())
if err != nil {
return err
}

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

if model := resp.Model; model != nil {
schema.Name = id.UpdateStrategyName
schema.KubernetesFleetManagerId = fleetupdatestrategies.NewFleetID(id.SubscriptionId, id.ResourceGroupName, id.FleetName).ID()
if model.Properties != nil {
schema.Stage = flattenKubernetesFleetUpdateStrategyStage(model.Properties.Strategy.Stages)
}
}

return metadata.Encode(&schema)
},
}
}
func (r KubernetesFleetUpdateStrategyResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Containers.FleetUpdateStrategiesClient

id, err := fleetupdatestrategies.ParseUpdateStrategyID(metadata.ResourceData.Id())
if err != nil {
return err
}

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

return nil
},
}
}

func expandKubernetesFleetUpdateStrategyStage(input []KubernetesFleetUpdateStrategyResourceUpdateStageSchema) []fleetupdatestrategies.UpdateStage {
output := make([]fleetupdatestrategies.UpdateStage, 0)
for _, stage := range input {
output = append(output, fleetupdatestrategies.UpdateStage{
Name: stage.Name,
AfterStageWaitInSeconds: pointer.FromInt64(stage.AfterStageWaitInSeconds),
Groups: expandKubernetesFleetUpdateStrategyGroup(stage.Group),
})
}
return output
}

func expandKubernetesFleetUpdateStrategyGroup(input []KubernetesFleetUpdateStrategyResourceUpdateGroupSchema) *[]fleetupdatestrategies.UpdateGroup {
output := make([]fleetupdatestrategies.UpdateGroup, 0)
for _, group := range input {
output = append(output, fleetupdatestrategies.UpdateGroup{
Name: group.Name,
})
}
return &output
}

func flattenKubernetesFleetUpdateStrategyStage(input []fleetupdatestrategies.UpdateStage) []KubernetesFleetUpdateStrategyResourceUpdateStageSchema {
output := make([]KubernetesFleetUpdateStrategyResourceUpdateStageSchema, 0)
for _, stage := range input {
output = append(output, KubernetesFleetUpdateStrategyResourceUpdateStageSchema{
Name: stage.Name,
AfterStageWaitInSeconds: pointer.ToInt64(stage.AfterStageWaitInSeconds),
Group: flattenKubernetesFleetUpdateStrategyGroup(stage.Groups),
})
}
return output

}

func flattenKubernetesFleetUpdateStrategyGroup(input *[]fleetupdatestrategies.UpdateGroup) []KubernetesFleetUpdateStrategyResourceUpdateGroupSchema {
output := make([]KubernetesFleetUpdateStrategyResourceUpdateGroupSchema, 0)
for _, group := range *input {
output = append(output, KubernetesFleetUpdateStrategyResourceUpdateGroupSchema{
Name: group.Name,
})
}
return output
}
Loading
Loading