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_automation_connection", azurerm_automation_connection_certificate, azurerm_automation_connection_classic_certificate and azurerm_automation_connection_service_pricipal` #6847

Merged
merged 3 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package automation

import (
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/automation/mgmt/2015-10-31/automation"
"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/automation/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/automation/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 resourceArmAutomationConnectionCertificate() *schema.Resource {
return &schema.Resource{
Create: resourceArmAutomationConnectionCertificateCreateUpdate,
Read: resourceArmAutomationConnectionCertificateRead,
Update: resourceArmAutomationConnectionCertificateCreateUpdate,
Delete: resourceArmAutomationConnectionCertificateDelete,

Importer: azSchema.ValidateResourceIDPriorToImportThen(func(id string) error {
_, err := parse.AutomationConnectionID(id)
return err
}, importAutomationConnection("Azure")),

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

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

"resource_group_name": azure.SchemaResourceGroupName(),

"automation_account_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateAutomationAccountName(),
},

"automation_certificate_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"subscription_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.IsUUID,
},

"description": {
Type: schema.TypeString,
Optional: true,
},
},
}
}

func resourceArmAutomationConnectionCertificateCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Automation.ConnectionClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

log.Printf("[INFO] preparing arguments for AzureRM Automation Connection creation.")

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
accountName := d.Get("automation_account_name").(string)

if d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, accountName, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing Automation Connection %q (Account %q / Resource Group %q): %s", name, accountName, resGroup, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_automation_connection_certificate", *existing.ID)
}
}

parameters := automation.ConnectionCreateOrUpdateParameters{
Name: &name,
ConnectionCreateOrUpdateProperties: &automation.ConnectionCreateOrUpdateProperties{
Description: utils.String(d.Get("description").(string)),
ConnectionType: &automation.ConnectionTypeAssociationProperty{
Name: utils.String("Azure"),
},
FieldDefinitionValues: map[string]*string{
"AutomationCertificateName": utils.String(d.Get("automation_certificate_name").(string)),
"SubscriptionID": utils.String(d.Get("subscription_id").(string)),
},
},
}

if _, err := client.CreateOrUpdate(ctx, resGroup, accountName, name, parameters); err != nil {
return err
}

read, err := client.Get(ctx, resGroup, accountName, name)
if err != nil {
return err
}

if read.ID == nil || *read.ID == "" {
return fmt.Errorf("empty or nil ID for Automation Connection '%s' (resource group %s) ID", name, resGroup)
}

d.SetId(*read.ID)

return resourceArmAutomationConnectionCertificateRead(d, meta)
}

func resourceArmAutomationConnectionCertificateRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Automation.ConnectionClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

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

resp, err := client.Get(ctx, id.ResourceGroup, id.AccountName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}

return fmt.Errorf("Read request on AzureRM Automation Connection '%s': %+v", id.Name, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("automation_account_name", id.AccountName)
d.Set("description", resp.Description)

if props := resp.ConnectionProperties; props != nil {
if v, ok := props.FieldDefinitionValues["AutomationCertificateName"]; ok {
d.Set("automation_certificate_name", v)
}
if v, ok := props.FieldDefinitionValues["SubscriptionID"]; ok {
d.Set("subscription_id", v)
}
}

return nil
}

func resourceArmAutomationConnectionCertificateDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Automation.ConnectionClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

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

resp, err := client.Delete(ctx, id.ResourceGroup, id.AccountName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return nil
}

return fmt.Errorf("deleting Automation Connection '%s': %+v", id.Name, err)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package automation

import (
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/automation/mgmt/2015-10-31/automation"
"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/automation/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/automation/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 resourceArmAutomationConnectionClassicCertificate() *schema.Resource {
return &schema.Resource{
Create: resourceArmAutomationConnectionClassicCertificateCreateUpdate,
Read: resourceArmAutomationConnectionClassicCertificateRead,
Update: resourceArmAutomationConnectionClassicCertificateCreateUpdate,
Delete: resourceArmAutomationConnectionClassicCertificateDelete,

Importer: azSchema.ValidateResourceIDPriorToImportThen(func(id string) error {
_, err := parse.AutomationConnectionID(id)
return err
}, importAutomationConnection("AzureClassicCertificate")),

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

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

"resource_group_name": azure.SchemaResourceGroupName(),

"automation_account_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateAutomationAccountName(),
},

"subscription_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.IsUUID,
},

"subscription_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"certificate_asset_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"description": {
Type: schema.TypeString,
Optional: true,
},
},
}
}

func resourceArmAutomationConnectionClassicCertificateCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Automation.ConnectionClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

log.Printf("[INFO] preparing arguments for AzureRM Automation Connection creation.")

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
accountName := d.Get("automation_account_name").(string)

if d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, accountName, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing Automation Connection %q (Account %q / Resource Group %q): %s", name, accountName, resGroup, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_automation_connection_classic_certificate", *existing.ID)
}
}

parameters := automation.ConnectionCreateOrUpdateParameters{
Name: &name,
ConnectionCreateOrUpdateProperties: &automation.ConnectionCreateOrUpdateProperties{
Description: utils.String(d.Get("description").(string)),
ConnectionType: &automation.ConnectionTypeAssociationProperty{
Name: utils.String("AzureClassicCertificate"),
},
FieldDefinitionValues: map[string]*string{
"SubscriptionName": utils.String(d.Get("subscription_name").(string)),
"SubscriptionId": utils.String(d.Get("subscription_id").(string)),
"CertificateAssetName": utils.String(d.Get("certificate_asset_name").(string)),
},
},
}

if _, err := client.CreateOrUpdate(ctx, resGroup, accountName, name, parameters); err != nil {
return err
}

read, err := client.Get(ctx, resGroup, accountName, name)
if err != nil {
return err
}

if read.ID == nil || *read.ID == "" {
return fmt.Errorf("empty or nil ID for Automation Connection '%s' (resource group %s) ID", name, resGroup)
}

d.SetId(*read.ID)

return resourceArmAutomationConnectionClassicCertificateRead(d, meta)
}

func resourceArmAutomationConnectionClassicCertificateRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Automation.ConnectionClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

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

resp, err := client.Get(ctx, id.ResourceGroup, id.AccountName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}

return fmt.Errorf("Read request on AzureRM Automation Connection '%s': %+v", id.Name, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("automation_account_name", id.AccountName)
d.Set("description", resp.Description)

if props := resp.ConnectionProperties; props != nil {
if v, ok := props.FieldDefinitionValues["CertificateAssetName"]; ok {
d.Set("certificate_asset_name", v)
}
if v, ok := props.FieldDefinitionValues["SubscriptionId"]; ok {
d.Set("subscription_id", v)
}
if v, ok := props.FieldDefinitionValues["SubscriptionName"]; ok {
d.Set("subscription_name", v)
}
}

return nil
}

func resourceArmAutomationConnectionClassicCertificateDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Automation.ConnectionClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

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

resp, err := client.Delete(ctx, id.ResourceGroup, id.AccountName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return nil
}

return fmt.Errorf("deleting Automation Connection '%s': %+v", id.Name, err)
}

return nil
}
Loading