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 5 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 @@ -21,6 +21,7 @@ import (
"github.com/Azure/azure-sdk-for-go/arm/servicebus"
"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 @@ -99,6 +100,8 @@ type ArmClient struct {
serviceBusSubscriptionsClient servicebus.SubscriptionsClient

keyVaultClient keyvault.VaultsClient

appsClient web.AppsClient
}

func withRequestLogging() autorest.SendDecorator {
Expand Down Expand Up @@ -458,6 +461,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

return &client, nil
}

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 @@ -129,6 +129,8 @@ func Provider() terraform.ResourceProvider {
"azurerm_sql_database": resourceArmSqlDatabase(),
"azurerm_sql_firewall_rule": resourceArmSqlFirewallRule(),
"azurerm_sql_server": resourceArmSqlServer(),

"azurerm_app_service": resourceArmAppService(),
},
}

Expand Down
162 changes: 162 additions & 0 deletions builtin/providers/azurerm/resource_arm_app_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
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: resourceArmWebAppCreateUpdate,
Read: resourceArmWebAppRead,
Update: resourceArmWebAppCreateUpdate,
Delete: resourceArmWebAppDelete,
Copy link
Contributor

Choose a reason for hiding this comment

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

minor: can we rename these methods to say AppService rather than WebApp?


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

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

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

},
"server_farm_id": {
Copy link
Contributor

Choose a reason for hiding this comment

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

can we rename this to 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("server_farm_id"); ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

as above - can we rename this to app_service_plan_id?

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 Site %s (resource group %s) ID", name, resGroup)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we rename Site -> App Service here?

}

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 web app details %s", id)
Copy link
Contributor

Choose a reason for hiding this comment

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

can we rename web app -> app service here?


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 Azure Web App %s: %s", name, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

can we rename Azure Web App -> AzureRM App Service Plan here?

}

d.Set("name", name)
d.Set("resource_group_name", resGroup)
Copy link
Contributor

Choose a reason for hiding this comment

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

the other fields need to be set here too:

  • always_on
  • tags
  • location
  • app_service_plan_id
  • ttl_in_seconds
  • force_dns_registration
  • skip_custom_domain_verification
  • skip_dns_registration


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 web app %s: %s", resGroup, name)
Copy link
Contributor

Choose a reason for hiding this comment

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

can we rename web app -> app service?


deleteMetrics := true
deleteEmptyServerFarm := true
skipDNSRegistration := 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 we're defaulting these values for the delete - should we do the same for the create?


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

return err
}
Loading