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

provider/azurerm: Create azure App services #12001

Closed
Closed
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
9 changes: 9 additions & 0 deletions builtin/providers/azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/Azure/azure-sdk-for-go/arm/sql"
"github.com/Azure/azure-sdk-for-go/arm/storage"
"github.com/Azure/azure-sdk-for-go/arm/trafficmanager"
"github.com/Azure/azure-sdk-for-go/arm/web"
mainStorage "github.com/Azure/azure-sdk-for-go/storage"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
Expand Down Expand Up @@ -101,6 +102,8 @@ type ArmClient struct {

keyVaultClient keyvault.VaultsClient

appsClient web.AppsClient

sqlElasticPoolsClient sql.ElasticPoolsClient
}

Expand Down Expand Up @@ -461,6 +464,12 @@ func (c *Config) getArmClient() (*ArmClient, error) {
kvc.Sender = autorest.CreateSender(withRequestLogging())
client.keyVaultClient = kvc

ac := web.NewAppsClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&ac.Client)
ac.Authorizer = spt
ac.Sender = autorest.CreateSender(withRequestLogging())
client.appsClient = ac

sqlepc := sql.NewElasticPoolsClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&sqlepc.Client)
sqlepc.Authorizer = spt
Expand Down
2 changes: 2 additions & 0 deletions builtin/providers/azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ func Provider() terraform.ResourceProvider {
"azurerm_sql_database": resourceArmSqlDatabase(),
"azurerm_sql_firewall_rule": resourceArmSqlFirewallRule(),
"azurerm_sql_server": resourceArmSqlServer(),

"azurerm_app_service": resourceArmAppService(),
},
}

Expand Down
164 changes: 164 additions & 0 deletions builtin/providers/azurerm/resource_arm_app_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package azurerm

import (
"fmt"
"log"
"net/http"

"github.com/Azure/azure-sdk-for-go/arm/web"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceArmAppService() *schema.Resource {
return &schema.Resource{
Create: resourceArmAppServiceCreateUpdate,
Read: resourceArmAppServiceRead,
Update: resourceArmAppServiceCreateUpdate,
Delete: resourceArmAppServiceDelete,

Schema: map[string]*schema.Schema{
"resource_group_name": {
Type: schema.TypeString,
Required: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this is a breaking change, this'll need a ForceNew: true here

ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"skip_dns_registration": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"skip_custom_domain_verification": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"force_dns_registration": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"ttl_in_seconds": {
Type: schema.TypeString,
Optional: true,
Default: "",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this'd be better as an int, rather than a string - given we can validate it against the API docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

},
"app_service_plan_id": {
Type: schema.TypeString,
Optional: true,
},
"always_on": {
Type: schema.TypeBool,
Optional: true,
},
"location": locationSchema(),
"tags": tagsSchema(),
},
}
}

func resourceArmWebAppCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
appClient := client.appsClient

log.Printf("[INFO] preparing arguments for Azure ARM Web App creation.")

resGroup := d.Get("resource_group_name").(string)
name := d.Get("name").(string)
location := d.Get("location").(string)
skipDNSRegistration := d.Get("skip_dns_registration").(bool)
skipCustomDomainVerification := d.Get("skip_custom_domain_verification").(bool)
forceDNSRegistration := d.Get("force_dns_registration").(bool)
ttlInSeconds := d.Get("ttl_in_seconds").(string)
tags := d.Get("tags").(map[string]interface{})

siteConfig := web.SiteConfig{}
if v, ok := d.GetOk("always_on"); ok {
alwaysOn := v.(bool)
siteConfig.AlwaysOn = &alwaysOn
}

siteProps := web.SiteProperties{
SiteConfig: &siteConfig,
}
if v, ok := d.GetOk("app_service_plan_id"); ok {
serverFarmID := v.(string)
siteProps.ServerFarmID = &serverFarmID
}

siteEnvelope := web.Site{
Location: &location,
Tags: expandTags(tags),
SiteProperties: &siteProps,
}

_, err := appClient.CreateOrUpdate(resGroup, name, siteEnvelope, &skipDNSRegistration, &skipCustomDomainVerification, &forceDNSRegistration, ttlInSeconds, make(chan struct{}))
if err != nil {
return err
}

read, err := appClient.Get(resGroup, name)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read App Service %s (resource group %s) ID", name, resGroup)
}

d.SetId(*read.ID)

return resourceArmWebAppRead(d, meta)
}

func resourceArmWebAppRead(d *schema.ResourceData, meta interface{}) error {
appClient := meta.(*ArmClient).appsClient

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

log.Printf("[DEBUG] Reading App Service details %s", id)

resGroup := id.ResourceGroup
name := id.Path["sites"]

resp, err := appClient.Get(resGroup, name)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on AzureRM App Service %s: %s", name, err)
}

d.Set("name", name)
d.Set("resource_group_name", resGroup)

return nil
}

func resourceArmWebAppDelete(d *schema.ResourceData, meta interface{}) error {
appClient := meta.(*ArmClient).appsClient

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["sites"]

log.Printf("[DEBUG] Deleting App Service %s: %s", resGroup, name)

deleteMetrics := true
deleteEmptyServerFarm := true
skipDNSRegistration := true

_, err = appClient.Delete(resGroup, name, &deleteMetrics, &deleteEmptyServerFarm, &skipDNSRegistration)

return err
}
Loading