Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Aris van Ommeren committed Jun 26, 2020
1 parent 71c1b1b commit 5b5e1ac
Showing 1 changed file with 87 additions and 87 deletions.
174 changes: 87 additions & 87 deletions azurerm/internal/services/web/resource_arm_static_site.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,41 +46,60 @@ func resourceArmStaticSite() *schema.Resource {

"location": azure.SchemaLocation(),

"tags": tags.Schema(),
"github_token": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
},

"github_configuration": {
Type: schema.TypeList,
"github_repo_url": {
Type: schema.TypeString,
Required: true,
},

"branch": {
Type: schema.TypeString,
Required: true,
},

"app_directory": {
Type: schema.TypeString,
Required: true,
},

"api_directory": {
Type: schema.TypeString,
Optional: true,
Default: "",
},

"artifact_directory": {
Type: schema.TypeString,
Optional: true,
Default: "",
},

"sku": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"repo_token": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
"repo_url": {
Type: schema.TypeString,
Required: true,
},
"branch": {
Type: schema.TypeString,
Required: true,
},
"app_location": {
Type: schema.TypeString,
Required: true,
},
"api_location": {
"tier": {
Type: schema.TypeString,
Optional: true,
Default: "Free",
},
"artifact_location": {
"size": {
Type: schema.TypeString,
Optional: true,
Default: "Free",
},
},
},
},

"tags": tags.Schema(),
},
}
}
Expand All @@ -93,13 +112,13 @@ func resourceArmStaticSiteCreateOrUpdate(d *schema.ResourceData, meta interface{
log.Printf("[INFO] preparing arguments for AzureRM Static Site creation.")

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

if d.IsNewResource() {
existing, err := client.GetStaticSite(ctx, resGroup, name)
existing, err := client.GetStaticSite(ctx, resourceGroup, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Static Site %q (Resource Group %q): %s", name, resGroup, err)
return fmt.Errorf("failed checking for presence of existing Static Site %q (Resource Group %q): %s", name, resourceGroup, err)
}
}

Expand All @@ -111,34 +130,48 @@ func resourceArmStaticSiteCreateOrUpdate(d *schema.ResourceData, meta interface{
location := azure.NormalizeLocation(d.Get("location").(string))
t := d.Get("tags").(map[string]interface{})

nameSku := "Free"
tierSku := "Free"
nameSku := d.Get("sku.0.name").(string)
tierSku := d.Get("sku.0.tier").(string)

staticSiteSkuDescription := &web.SkuDescription{Name: &nameSku, Tier: &tierSku}

staticSiteType := "Microsoft.Web/staticSites"
githubRepoURL := d.Get("github_repo_url").(string)
branch := d.Get("branch").(string)
githubToken := d.Get("github_token").(string)

staticSiteSourceControlRaw := d.Get("github_configuration").([]interface{})
staticSiteSourceControl := expandStaticSiteSourceControl(staticSiteSourceControlRaw)
appDirectory := d.Get("app_directory").(string)
apiDirectory := d.Get("api_directory").(string)
artifactDirectory := d.Get("artifact_directory").(string)

staticSiteSourceControl := &web.StaticSite{
RepositoryURL: &githubRepoURL,
Branch: &branch,
RepositoryToken: &githubToken,
BuildProperties: &web.StaticSiteBuildProperties{
AppLocation: &appDirectory,
APILocation: &apiDirectory,
AppArtifactLocation: &artifactDirectory,
},
}

siteEnvelope := web.StaticSiteARMResource{
Sku: staticSiteSkuDescription,
Type: &staticSiteType,
StaticSite: staticSiteSourceControl,
Location: &location,
Tags: tags.Expand(t),
}

_, err := client.CreateOrUpdateStaticSite(ctx, resGroup, name, siteEnvelope)
_, err := client.CreateOrUpdateStaticSite(ctx, resourceGroup, name, siteEnvelope)
if err != nil {
return fmt.Errorf("Error creating Static Site %q (Resource Group %q): %s", name, resGroup, err)
return fmt.Errorf("failed creating Static Site %q (Resource Group %q): %s", name, resourceGroup, err)
}

read, err := client.GetStaticSite(ctx, resGroup, name)
read, err := client.GetStaticSite(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error retrieving Static Site %q (Resource Group %q): %s", name, resGroup, err)
return fmt.Errorf("failed retrieving Static Site %q (Resource Group %q): %s", name, resourceGroup, err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read Static Site %q (resource group %q) ID", name, resGroup)
return fmt.Errorf("cannot read Static Site %q (resource group %q) ID", name, resourceGroup)
}

d.SetId(*read.ID)
Expand All @@ -163,7 +196,7 @@ func resourceArmStaticSiteRead(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on AzureRM Static Site %q: %+v", id.Name, err)
return fmt.Errorf("failed making Read request on AzureRM Static Site %q: %+v", id.Name, err)
}
d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
Expand All @@ -174,7 +207,7 @@ func resourceArmStaticSiteRead(d *schema.ResourceData, meta interface{}) error {

sc := flattenStaticSiteSourceControl(resp.StaticSite, d)
if err := d.Set("github_configuration", sc); err != nil {
return fmt.Errorf("Error setting `github_configuration`: %s", err)
return fmt.Errorf("failed setting `github_configuration`: %s", err)
}

return tags.FlattenAndSet(d, resp.Tags)
Expand Down Expand Up @@ -208,19 +241,19 @@ func flattenStaticSiteSourceControl(input *web.StaticSite, d *schema.ResourceDat
return []interface{}{}
}

repoURL := ""
githubRepoURL := ""
if input.RepositoryURL != nil {
repoURL = *input.RepositoryURL
githubRepoURL = *input.RepositoryURL
}
branch := ""
if input.Branch != nil && *input.Branch != "" {
branch = *input.Branch
}

repoToken := ""
apiLocation := ""
appLocation := ""
artifactLocation := ""
githubToken := ""
apiDirectory := ""
appDirectory := ""
artifactDirectory := ""
if sc, ok := d.GetOk("github_configuration"); ok {
var val []interface{}

Expand All @@ -230,54 +263,21 @@ func flattenStaticSiteSourceControl(input *web.StaticSite, d *schema.ResourceDat

if len(val) > 0 && val[0] != nil {
raw := val[0].(map[string]interface{})
repoToken = raw["repo_token"].(string)
apiLocation = raw["api_location"].(string)
appLocation = raw["app_location"].(string)
artifactLocation = raw["artifact_location"].(string)
githubToken = raw["github_token"].(string)
apiDirectory = raw["api_directory"].(string)
appDirectory = raw["app_directory"].(string)
artifactDirectory = raw["artifact_directory"].(string)
}
}

return []interface{}{
map[string]interface{}{
"repo_url": repoURL,
"branch": branch,
"repo_token": repoToken,
"api_location": apiLocation,
"artifact_location": artifactLocation,
"app_location": appLocation,
"github_repo_url": githubRepoURL,
"branch": branch,
"github_token": githubToken,
"api_directory": apiDirectory,
"artifact_directory": artifactDirectory,
"app_directory": appDirectory,
},
}
}

func expandStaticSiteSourceControl(input []interface{}) *web.StaticSite {
if len(input) == 0 {
return nil
}
sourceControl := input[0].(map[string]interface{})
repoURL := sourceControl["repo_url"].(string)
branch := sourceControl["branch"].(string)
repoToken := sourceControl["repo_token"].(string)

appLocation := sourceControl["app_location"].(string)
apiLocation := ""
if v, ok := sourceControl["api_location"]; ok {
apiLocation = v.(string)
}
artifactLocation := ""
if v, ok := sourceControl["artifact_location"]; ok {
artifactLocation = v.(string)
}

staticSite := &web.StaticSite{
RepositoryURL: &repoURL,
Branch: &branch,
RepositoryToken: &repoToken,
BuildProperties: &web.StaticSiteBuildProperties{
AppLocation: &appLocation,
APILocation: &apiLocation,
AppArtifactLocation: &artifactLocation,
},
}

return staticSite
}

0 comments on commit 5b5e1ac

Please sign in to comment.