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_build_deployment #16730

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_mysql_association": resourceSpringCloudAppMysqlAssociation(),
"azurerm_spring_cloud_app_redis_association": resourceSpringCloudAppRedisAssociation(),
"azurerm_spring_cloud_builder": resourceSpringCloudBuildServiceBuilder(),
"azurerm_spring_cloud_build_deployment": resourceSpringCloudBuildDeployment(),
"azurerm_spring_cloud_build_pack_binding": resourceSpringCloudBuildPackBinding(),
"azurerm_spring_cloud_certificate": resourceSpringCloudCertificate(),
"azurerm_spring_cloud_configuration_service": resourceSpringCloudConfigurationService(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
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 resourceSpringCloudBuildDeployment() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceSpringCloudBuildDeploymentCreateUpdate,
Read: resourceSpringCloudBuildDeploymentRead,
Update: resourceSpringCloudBuildDeploymentCreateUpdate,
Delete: resourceSpringCloudBuildDeploymentDelete,

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

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

"environment_variables": {
Type: pluginsdk.TypeMap,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
},

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

"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 resourceSpringCloudBuildDeploymentCreateUpdate(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_build_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.BuildResultUserSourceInfo{
BuildResultID: utils.String(d.Get("build_result_id").(string)),
},
DeploymentSettings: &appplatform.DeploymentSettings{
EnvironmentVariables: expandSpringCloudDeploymentEnvironmentVariables(d.Get("environment_variables").(map[string]interface{})),
ResourceRequests: expandSpringCloudBuildDeploymentResourceRequests(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 resourceSpringCloudBuildDeploymentRead(d, meta)
}

func resourceSpringCloudBuildDeploymentRead(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.AsBuildResultUserSourceInfo(); ok && source != nil {
d.Set("build_result_id", source.BuildResultID)
}
}

return nil
}

func resourceSpringCloudBuildDeploymentDelete(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 expandSpringCloudBuildDeploymentResourceRequests(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