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_spring_cloud_container_deployment #16181

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
1 change: 1 addition & 0 deletions internal/services/springcloud/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (r Registration) SupportedResources() map[string]*pluginsdk.Resource {
"azurerm_spring_cloud_app_redis_association": resourceSpringCloudAppRedisAssociation(),
"azurerm_spring_cloud_certificate": resourceSpringCloudCertificate(),
"azurerm_spring_cloud_custom_domain": resourceSpringCloudCustomDomain(),
"azurerm_spring_cloud_container_deployment": resourceSpringCloudContainerDeployment(),
"azurerm_spring_cloud_java_deployment": resourceSpringCloudJavaDeployment(),
"azurerm_spring_cloud_service": resourceSpringCloudService(),
"azurerm_spring_cloud_storage": resourceSpringCloudStorage(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,310 @@
package springcloud

import (
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2022-03-01-preview/appplatform"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/springcloud/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/springcloud/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func resourceSpringCloudContainerDeployment() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceSpringCloudContainerDeploymentCreateUpdate,
Read: resourceSpringCloudContainerDeploymentRead,
Update: resourceSpringCloudContainerDeploymentCreateUpdate,
Delete: resourceSpringCloudContainerDeploymentDelete,

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.SpringCloudDeploymentID(id)
return err
}),

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(30 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.SpringCloudDeploymentName,
},

"spring_cloud_app_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.SpringCloudAppID,
},

"image": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"server": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"arguments": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
ms-henglu marked this conversation as resolved.
Show resolved Hide resolved
},

"commands": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
ms-henglu marked this conversation as resolved.
Show resolved Hide resolved
},

"environment_variables": {
Type: pluginsdk.TypeMap,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
ms-henglu marked this conversation as resolved.
Show resolved Hide resolved
},

"instance_count": {
Type: pluginsdk.TypeInt,
Optional: true,
Default: 1,
ValidateFunc: validation.IntBetween(1, 500),
},

"language_framework": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"springboot",
}, false),
},

"quota": {
Type: pluginsdk.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"cpu": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"500m",
"1",
"2",
"3",
"4",
}, false),
},

"memory": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"512Mi",
"1Gi",
"2Gi",
"3Gi",
"4Gi",
"5Gi",
"6Gi",
"7Gi",
"8Gi",
}, false),
},
},
},
},
},
}
}

func resourceSpringCloudContainerDeploymentCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).AppPlatform.DeploymentsClient
servicesClient := meta.(*clients.Client).AppPlatform.ServicesClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

appId, err := parse.SpringCloudAppID(d.Get("spring_cloud_app_id").(string))
if err != nil {
return err
}

id := parse.NewSpringCloudDeploymentID(subscriptionId, appId.ResourceGroup, appId.SpringName, appId.AppName, d.Get("name").(string))

if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.SpringName, id.AppName, id.DeploymentName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_spring_cloud_container_deployment", id.ID())
}
}

service, err := servicesClient.Get(ctx, appId.ResourceGroup, appId.SpringName)
if err != nil {
return fmt.Errorf("checking for presence of existing %s: %+v", appId, err)
}
if service.Sku == nil || service.Sku.Name == nil || service.Sku.Tier == nil {
return fmt.Errorf("invalid `sku` for Spring Cloud Service %q (Resource Group %q)", appId.SpringName, appId.ResourceGroup)
}

deployment := appplatform.DeploymentResource{
Sku: &appplatform.Sku{
Name: service.Sku.Name,
Tier: service.Sku.Tier,
Capacity: utils.Int32(int32(d.Get("instance_count").(int))),
},
Properties: &appplatform.DeploymentResourceProperties{
Source: appplatform.CustomContainerUserSourceInfo{
CustomContainer: &appplatform.CustomContainer{
Server: utils.String(d.Get("server").(string)),
ContainerImage: utils.String(d.Get("image").(string)),
Command: utils.ExpandStringSlice(d.Get("commands").([]interface{})),
Args: utils.ExpandStringSlice(d.Get("arguments").([]interface{})),
LanguageFramework: utils.String(d.Get("language_framework").(string)),
},
},
DeploymentSettings: &appplatform.DeploymentSettings{
EnvironmentVariables: expandSpringCloudDeploymentEnvironmentVariables(d.Get("environment_variables").(map[string]interface{})),
ResourceRequests: expandSpringCloudContainerDeploymentResourceRequests(d.Get("quota").([]interface{})),
},
},
}

future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.SpringName, id.AppName, id.DeploymentName, deployment)
if err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation of %s: %+v", id, err)
}

d.SetId(id.ID())

return resourceSpringCloudContainerDeploymentRead(d, meta)
}

func resourceSpringCloudContainerDeploymentRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).AppPlatform.DeploymentsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.SpringCloudDeploymentID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.SpringName, id.AppName, id.DeploymentName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Spring Cloud deployment %q does not exist - removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("reading %s: %+v", id, err)
}

d.Set("name", id.DeploymentName)
d.Set("spring_cloud_app_id", parse.NewSpringCloudAppID(id.SubscriptionId, id.ResourceGroup, id.SpringName, id.AppName).ID())
if resp.Sku != nil {
d.Set("instance_count", resp.Sku.Capacity)
}
if resp.Properties != nil {
if settings := resp.Properties.DeploymentSettings; settings != nil {
d.Set("environment_variables", flattenSpringCloudDeploymentEnvironmentVariables(settings.EnvironmentVariables))
if err := d.Set("quota", flattenSpringCloudDeploymentResourceRequests(settings.ResourceRequests)); err != nil {
return fmt.Errorf("setting `quota`: %+v", err)
}
}
if source, ok := resp.Properties.Source.AsCustomContainerUserSourceInfo(); ok && source != nil {
if container := source.CustomContainer; container != nil {
d.Set("server", container.Server)
d.Set("image", container.ContainerImage)
d.Set("arguments", utils.FlattenStringSlice(container.Args))
d.Set("commands", utils.FlattenStringSlice(container.Command))
d.Set("language_framework", container.LanguageFramework)
}
}
}

return nil
}

func resourceSpringCloudContainerDeploymentDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).AppPlatform.DeploymentsClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.SpringCloudDeploymentID(d.Id())
if err != nil {
return err
}

future, err := client.Delete(ctx, id.ResourceGroup, id.SpringName, id.AppName, id.DeploymentName)
if err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of %s: %+v", id, err)
}

return nil
}

func expandSpringCloudContainerDeploymentResourceRequests(input []interface{}) *appplatform.ResourceRequests {
if len(input) == 0 || input[0] == nil {
return nil
}
cpuResult := "1"
memResult := "1Gi"

v := input[0].(map[string]interface{})
if cpuNew := v["cpu"].(string); cpuNew != "" {
cpuResult = cpuNew
}

if memoryNew := v["memory"].(string); memoryNew != "" {
memResult = memoryNew
}

result := appplatform.ResourceRequests{
CPU: utils.String(cpuResult),
Memory: utils.String(memResult),
}

return &result
}
Loading