|
| 1 | +package appplatform |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2019-05-01-preview/appplatform" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 10 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" |
| 11 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" |
| 12 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" |
| 13 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/appplatform/parse" |
| 14 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/appplatform/validate" |
| 15 | + azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema" |
| 16 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" |
| 17 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" |
| 18 | +) |
| 19 | + |
| 20 | +func resourceArmSpringCloudApp() *schema.Resource { |
| 21 | + return &schema.Resource{ |
| 22 | + Create: resourceArmSpringCloudAppCreate, |
| 23 | + Read: resourceArmSpringCloudAppRead, |
| 24 | + Delete: resourceArmSpringCloudAppDelete, |
| 25 | + |
| 26 | + Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { |
| 27 | + _, err := parse.SpringCloudAppID(id) |
| 28 | + return err |
| 29 | + }), |
| 30 | + |
| 31 | + Timeouts: &schema.ResourceTimeout{ |
| 32 | + Create: schema.DefaultTimeout(30 * time.Minute), |
| 33 | + Read: schema.DefaultTimeout(5 * time.Minute), |
| 34 | + Delete: schema.DefaultTimeout(30 * time.Minute), |
| 35 | + }, |
| 36 | + |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "name": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Required: true, |
| 41 | + ForceNew: true, |
| 42 | + ValidateFunc: validate.SpringCloudAppName, |
| 43 | + }, |
| 44 | + |
| 45 | + "resource_group_name": azure.SchemaResourceGroupName(), |
| 46 | + |
| 47 | + "service_name": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Required: true, |
| 50 | + ForceNew: true, |
| 51 | + ValidateFunc: validate.SpringCloudServiceName, |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func resourceArmSpringCloudAppCreate(d *schema.ResourceData, meta interface{}) error { |
| 58 | + client := meta.(*clients.Client).AppPlatform.AppsClient |
| 59 | + ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) |
| 60 | + defer cancel() |
| 61 | + |
| 62 | + name := d.Get("name").(string) |
| 63 | + resourceGroup := d.Get("resource_group_name").(string) |
| 64 | + serviceName := d.Get("service_name").(string) |
| 65 | + |
| 66 | + existing, err := client.Get(ctx, resourceGroup, serviceName, name, "") |
| 67 | + if err != nil { |
| 68 | + if !utils.ResponseWasNotFound(existing.Response) { |
| 69 | + return fmt.Errorf("checking for presence of existing Spring Cloud App %q (Spring Cloud Service %q / Resource Group %q): %+v", name, serviceName, resourceGroup, err) |
| 70 | + } |
| 71 | + } |
| 72 | + if existing.ID != nil && *existing.ID != "" { |
| 73 | + return tf.ImportAsExistsError("azurerm_spring_cloud_app", *existing.ID) |
| 74 | + } |
| 75 | + |
| 76 | + future, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, name, appplatform.AppResource{}) |
| 77 | + if err != nil { |
| 78 | + return fmt.Errorf("creating Spring Cloud App %q (Spring Cloud Service %q / Resource Group %q): %+v", name, serviceName, resourceGroup, err) |
| 79 | + } |
| 80 | + if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { |
| 81 | + return fmt.Errorf("waiting for creation of Spring Cloud App %q (Spring Cloud Service %q / Resource Group %q): %+v", name, serviceName, resourceGroup, err) |
| 82 | + } |
| 83 | + |
| 84 | + resp, err := client.Get(ctx, resourceGroup, serviceName, name, "") |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("retrieving Spring Cloud App %q (Spring Cloud Service %q / Resource Group %q): %+v", name, serviceName, resourceGroup, err) |
| 87 | + } |
| 88 | + if resp.ID == nil || *resp.ID == "" { |
| 89 | + return fmt.Errorf("read Spring Cloud App %q (Spring Cloud Service %q / Resource Group %q) ID", name, serviceName, resourceGroup) |
| 90 | + } |
| 91 | + d.SetId(*resp.ID) |
| 92 | + |
| 93 | + return resourceArmSpringCloudAppRead(d, meta) |
| 94 | +} |
| 95 | + |
| 96 | +func resourceArmSpringCloudAppRead(d *schema.ResourceData, meta interface{}) error { |
| 97 | + client := meta.(*clients.Client).AppPlatform.AppsClient |
| 98 | + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) |
| 99 | + defer cancel() |
| 100 | + |
| 101 | + id, err := parse.SpringCloudAppID(d.Id()) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + resp, err := client.Get(ctx, id.ResourceGroup, id.ServiceName, id.Name, "") |
| 107 | + if err != nil { |
| 108 | + if utils.ResponseWasNotFound(resp.Response) { |
| 109 | + log.Printf("[INFO] Spring Cloud App %q does not exist - removing from state", d.Id()) |
| 110 | + d.SetId("") |
| 111 | + return nil |
| 112 | + } |
| 113 | + return fmt.Errorf("reading Spring Cloud App %q (Spring Cloud Service %q / Resource Group %q): %+v", id.Name, id.ServiceName, id.ResourceGroup, err) |
| 114 | + } |
| 115 | + |
| 116 | + d.Set("name", resp.Name) |
| 117 | + d.Set("resource_group_name", id.ResourceGroup) |
| 118 | + d.Set("service_name", id.ServiceName) |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func resourceArmSpringCloudAppDelete(d *schema.ResourceData, meta interface{}) error { |
| 124 | + client := meta.(*clients.Client).AppPlatform.AppsClient |
| 125 | + ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) |
| 126 | + defer cancel() |
| 127 | + |
| 128 | + id, err := parse.SpringCloudAppID(d.Id()) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + |
| 133 | + if _, err := client.Delete(ctx, id.ResourceGroup, id.ServiceName, id.Name); err != nil { |
| 134 | + return fmt.Errorf("deleting Spring Cloud App %q (Spring Cloud Service %q / Resource Group %q): %+v", id.Name, id.ServiceName, id.ResourceGroup, err) |
| 135 | + } |
| 136 | + |
| 137 | + return nil |
| 138 | +} |
0 commit comments