-
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.
new resource
azurerm_spring_cloud_certificate
(#8067)
- Loading branch information
Showing
8 changed files
with
659 additions
and
6 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
38 changes: 38 additions & 0 deletions
38
azurerm/internal/services/appplatform/parse/spring_cloud_certificate.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,38 @@ | ||
package parse | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
) | ||
|
||
type SpringCloudCertificateId struct { | ||
ResourceGroup string | ||
ServiceName string | ||
Name string | ||
} | ||
|
||
func SpringCloudCertificateID(input string) (*SpringCloudCertificateId, error) { | ||
id, err := azure.ParseAzureResourceID(input) | ||
if err != nil { | ||
return nil, fmt.Errorf("parsing Spring Cloud Certificate ID %q: %+v", input, err) | ||
} | ||
|
||
cert := SpringCloudCertificateId{ | ||
ResourceGroup: id.ResourceGroup, | ||
} | ||
|
||
if cert.ServiceName, err = id.PopSegment("Spring"); err != nil { | ||
return nil, err | ||
} | ||
|
||
if cert.Name, err = id.PopSegment("certificates"); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := id.ValidateNoEmptySegments(input); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &cert, nil | ||
} |
88 changes: 88 additions & 0 deletions
88
azurerm/internal/services/appplatform/parse/spring_cloud_certificate_test.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,88 @@ | ||
package parse | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestSpringCloudCertificateID(t *testing.T) { | ||
testData := []struct { | ||
Name string | ||
Input string | ||
Expected *SpringCloudCertificateId | ||
}{ | ||
{ | ||
Name: "Empty", | ||
Input: "", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "No Resource Groups Segment", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "No Resource Groups Value", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "Resource Group ID", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "No Spring Value", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.AppPlatform/Spring/", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "Missing Certificates Segment", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.AppPlatform/Spring/spring1/", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "Missing Certificates Value", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.AppPlatform/Spring/spring1/certificates/", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "Spring Cloud Certificate ID", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.AppPlatform/Spring/spring1/certificates/cert1", | ||
Expected: &SpringCloudCertificateId{ | ||
ResourceGroup: "resGroup1", | ||
ServiceName: "spring1", | ||
Name: "cert1", | ||
}, | ||
}, | ||
{ | ||
Name: "Wrong Casing", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.AppPlatform/Spring/spring1/Certificates/cert1", | ||
Expected: nil, | ||
}, | ||
} | ||
|
||
for _, v := range testData { | ||
t.Logf("[DEBUG] Testing %q", v.Name) | ||
|
||
actual, err := SpringCloudCertificateID(v.Input) | ||
if err != nil { | ||
if v.Expected == nil { | ||
continue | ||
} | ||
|
||
t.Fatalf("Expected a value but got an error: %s", err) | ||
} | ||
|
||
if actual.ResourceGroup != v.Expected.ResourceGroup { | ||
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup) | ||
} | ||
|
||
if actual.ServiceName != v.Expected.ServiceName { | ||
t.Fatalf("Expected %q but got %q for ServiceName", v.Expected.ServiceName, actual.ServiceName) | ||
} | ||
|
||
if actual.Name != v.Expected.Name { | ||
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) | ||
} | ||
} | ||
} |
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
149 changes: 149 additions & 0 deletions
149
azurerm/internal/services/appplatform/spring_cloud_certificate_resource.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,149 @@ | ||
package appplatform | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2019-05-01-preview/appplatform" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/appplatform/parse" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/appplatform/validate" | ||
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmSpringCloudCertificate() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmSpringCloudCertificateCreate, | ||
Read: resourceArmSpringCloudCertificateRead, | ||
Delete: resourceArmSpringCloudCertificateDelete, | ||
|
||
Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { | ||
_, err := parse.SpringCloudCertificateID(id) | ||
return err | ||
}), | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(30 * time.Minute), | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
Delete: schema.DefaultTimeout(30 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
|
||
"resource_group_name": azure.SchemaResourceGroupName(), | ||
|
||
"service_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.SpringCloudServiceName, | ||
}, | ||
|
||
"key_vault_certificate_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: azure.ValidateKeyVaultChildId, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmSpringCloudCertificateCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).AppPlatform.CertificatesClient | ||
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
name := d.Get("name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
serviceName := d.Get("service_name").(string) | ||
|
||
existing, err := client.Get(ctx, resourceGroup, serviceName, name) | ||
if err != nil { | ||
if !utils.ResponseWasNotFound(existing.Response) { | ||
return fmt.Errorf("checking for presence of existing Spring Cloud Service Certificate %q (Spring Cloud Service %q / Resource Group %q): %+v", name, serviceName, resourceGroup, err) | ||
} | ||
} | ||
if existing.ID != nil && *existing.ID != "" { | ||
return tf.ImportAsExistsError("azurerm_spring_cloud_certificate", *existing.ID) | ||
} | ||
|
||
keyVaultCertificateId, _ := azure.ParseKeyVaultChildID(d.Get("key_vault_certificate_id").(string)) | ||
cert := appplatform.CertificateResource{ | ||
Properties: &appplatform.CertificateProperties{ | ||
VaultURI: &keyVaultCertificateId.KeyVaultBaseUrl, | ||
KeyVaultCertName: &keyVaultCertificateId.Name, | ||
}, | ||
} | ||
|
||
if _, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, name, cert); err != nil { | ||
return fmt.Errorf("creating Spring Cloud Certificate %q (Spring Cloud Service %q / Resource Group %q): %+v", name, serviceName, resourceGroup, err) | ||
} | ||
|
||
resp, err := client.Get(ctx, resourceGroup, serviceName, name) | ||
if err != nil { | ||
return fmt.Errorf("retrieving Spring Cloud Certificate %q (Spring Cloud Service %q / Resource Group %q): %+v", name, serviceName, resourceGroup, err) | ||
} | ||
if resp.ID == nil || *resp.ID == "" { | ||
return fmt.Errorf("read Spring Cloud Certificate %q (Spring Cloud Service %q / Resource Group %q) ID", name, serviceName, resourceGroup) | ||
} | ||
d.SetId(*resp.ID) | ||
|
||
return resourceArmSpringCloudCertificateRead(d, meta) | ||
} | ||
|
||
func resourceArmSpringCloudCertificateRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).AppPlatform.CertificatesClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parse.SpringCloudCertificateID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resp, err := client.Get(ctx, id.ResourceGroup, id.ServiceName, id.Name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
log.Printf("[INFO] Spring Cloud Certificate %q does not exist - removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("reading Spring Cloud Certificate %q (Spring Cloud Service %q / Resource Group %q): %+v", id.Name, id.ServiceName, id.ResourceGroup, err) | ||
} | ||
|
||
d.Set("name", resp.Name) | ||
d.Set("resource_group_name", id.ResourceGroup) | ||
d.Set("service_name", id.ServiceName) | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmSpringCloudCertificateDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).AppPlatform.CertificatesClient | ||
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parse.SpringCloudCertificateID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, err := client.Delete(ctx, id.ResourceGroup, id.ServiceName, id.Name); err != nil { | ||
return fmt.Errorf("deleting Spring Cloud Certificate %q (Spring Cloud Service %q / Resource Group %q): %+v", id.Name, id.ServiceName, id.ResourceGroup, err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.