Skip to content

Commit

Permalink
finished refactor to testable level
Browse files Browse the repository at this point in the history
  • Loading branch information
jackofallops committed Jul 10, 2020
1 parent 5f09f1c commit a3fba41
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 121 deletions.
127 changes: 57 additions & 70 deletions azurerm/internal/services/web/resource_arm_static_site.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,16 @@ func resourceArmStaticSite() *schema.Resource {
Default: "",
},

"sku": {
Type: schema.TypeList,
"sku_tier": {
Type: schema.TypeString,
Optional: true,
Default: "Free",
},

"sku_size": {
Type: schema.TypeString,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"tier": {
Type: schema.TypeString,
Optional: true,
Default: "Free",
},
"size": {
Type: schema.TypeString,
Optional: true,
Default: "Free",
},
},
},
Default: "Free",
},

"tags": tags.Schema(),
Expand Down Expand Up @@ -130,10 +122,10 @@ func resourceArmStaticSiteCreateOrUpdate(d *schema.ResourceData, meta interface{
location := azure.NormalizeLocation(d.Get("location").(string))
t := d.Get("tags").(map[string]interface{})

nameSku := d.Get("sku.0.name").(string)
tierSku := d.Get("sku.0.tier").(string)
skuName := d.Get("sku_size").(string)
skuTier := d.Get("sku_tier").(string)

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

githubRepoURL := d.Get("github_repo_url").(string)
branch := d.Get("branch").(string)
Expand Down Expand Up @@ -205,9 +197,51 @@ func resourceArmStaticSiteRead(d *schema.ResourceData, meta interface{}) error {
d.Set("location", azure.NormalizeLocation(*location))
}

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

branch := ""
if v := resp.StaticSite.Branch; v != nil {
branch = *v
}
d.Set("branch", branch)

if resp.StaticSite.BuildProperties != nil {
appLocation := ""
if v := resp.StaticSite.BuildProperties.AppLocation; v != nil {
appLocation = *v
}
d.Set("app_directory", appLocation)

apiLocation := ""
if v := resp.StaticSite.BuildProperties.APILocation; v != nil {
apiLocation = *v
}
d.Set("api_directory", apiLocation)

appArtifactLocation := ""
if v := resp.StaticSite.BuildProperties.AppArtifactLocation; v != nil {
appArtifactLocation = *v
}
d.Set("artifact_directory", appArtifactLocation)
}
}
if resp.Sku != nil {
skuName := "Free"
if v := resp.Sku.Name; v != nil {
skuName = *v
}
d.Set("sku_size", skuName)

skuTier := "Free"
if v := resp.Sku.Tier; v != nil {
skuTier = *v
}
d.Set("sku_tier", skuTier)
}

return tags.FlattenAndSet(d, resp.Tags)
Expand All @@ -234,50 +268,3 @@ func resourceArmStaticSiteDelete(d *schema.ResourceData, meta interface{}) error

return nil
}

func flattenStaticSiteSourceControl(input *web.StaticSite, d *schema.ResourceData) []interface{} {
if input == nil {
log.Printf("[DEBUG] SiteSourceControlProperties is nil")
return []interface{}{}
}

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

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

if v, ok := sc.([]interface{}); ok {
val = v
}

if len(val) > 0 && val[0] != nil {
raw := val[0].(map[string]interface{})
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{}{
"github_repo_url": githubRepoURL,
"branch": branch,
"github_token": githubToken,
"api_directory": apiDirectory,
"artifact_directory": artifactDirectory,
"app_directory": appDirectory,
},
}
}
2 changes: 1 addition & 1 deletion azurerm/internal/services/web/tests/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import (
)

func skipStaticSite() bool {
return os.Getenv("ARM_TEST_GITHUB_TOKEN") == ""
return os.Getenv("ARM_TEST_GITHUB_TOKEN") == "" || os.Getenv("ARM_TEST_GITHUB_REPO") == ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestAccAzureRMStaticSite_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_static_site", "test")

if ok := skipStaticSite(); ok {
t.Skip("Skipping as `ARM_TEST_GITHUB_TOKEN` was not specified")
t.Skip("Skipping as both `ARM_TEST_GITHUB_TOKEN` and `ARM_TEST_GITHUB_REPO` were not specified")
}

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -31,10 +31,10 @@ func TestAccAzureRMStaticSite_basic(t *testing.T) {
),
},
data.ImportStep(
"github_configuration.0.api_location",
"github_configuration.0.app_location",
"github_configuration.0.artifact_location",
"github_configuration.0.repo_token"),
"api_directory",
"app_directory",
"artifact_directory",
"github_token"),
},
})
}
Expand Down Expand Up @@ -134,18 +134,14 @@ resource "azurerm_static_site" "test" {
name = "acctestSS-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
github_configuration {
repo_url = "https://github.com/aristosvo/azure-static-web-app"
branch = "master"
repo_token = "%s"
app_location = "/"
api_location = ""
artifact_location = "dist/angular-basic"
}
github_repo_url = "%s"
branch = "master"
github_token = "%s"
app_directory = "/"
api_directory = ""
artifact_directory = "dist/angular-basic"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, os.Getenv("ARM_TEST_GITHUB_TOKEN"))
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, os.Getenv("ARM_TEST_GITHUB_REPO"), os.Getenv("ARM_TEST_GITHUB_TOKEN"))
}

func testAccAzureRMStaticSite_requiresImport(data acceptance.TestData) string {
Expand All @@ -157,16 +153,12 @@ resource "azurerm_static_site" "import" {
name = azurerm_static_site.test.name
location = azurerm_static_site.test.location
resource_group_name = azurerm_static_site.test.resource_group_name
github_configuration {
repo_url = azurerm_static_site.test.github_configuration.0.repo_url
branch = azurerm_static_site.test.github_configuration.0.branch
repo_token = azurerm_static_site.test.github_configuration.0.repo_token
app_location = azurerm_static_site.test.github_configuration.0.app_location
api_location = azurerm_static_site.test.github_configuration.0.api_location
artifact_location = azurerm_static_site.test.github_configuration.0.artifact_location
}
github_repo_url = azurerm_static_site.test.github_repo_url
branch = azurerm_static_site.test.branch
github_token = azurerm_static_site.test.github_token
app_directory = azurerm_static_site.test.app_directory
api_directory = azurerm_static_site.test.api_directory
artifact_directory = azurerm_static_site.test.artifact_directory
}
`, template)
}
38 changes: 14 additions & 24 deletions website/docs/r/static_site.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ subcategory: "App Service (Web Apps)"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_static_site"
description: |-
Manages a Static Web App.
Manages an App Service Static Site.
---

# azurerm_static_site

Manages a Static Web App.
Manages an App Service Static Site.

## Example Usage

Expand All @@ -17,13 +17,10 @@ resource "azurerm_static_site" "example" {
name = "example"
resource_group_name = "example"
location = "West Europe"
github_configuration {
repo_token = "personal-access-token-github"
repo_url = "https://github.com/example/static-web-app-example"
branch = "master"
app_location = "/"
}
github_repo_url = "https://github.com/example/static-web-app-example"
github_token = "personal-access-token-github"
branch = "release"
app_directory = "/"
}
```

Expand All @@ -37,27 +34,20 @@ The following arguments are supported:

* `resource_group_name` - (Required) The name of the Resource Group where the Static Web App should exist. Changing this forces a new Static Web App to be created.

* `github_configuration` - (Required) A `github_configuration` block as defined below.

---

* `tags` - (Optional) A mapping of tags which should be assigned to the Static Web App.

---

A `github_configuration` block supports the following:

* `app_location` - (Required) The path to the Static Web App site code within the repository.
* `app_directory` - (Required) The path to the Static Web App site code within the repository.

* `branch` - (Required) The target branch in the repository.

* `repo_token` - (Required) A user's github repository token. This is used to setup the Github Actions workflow file and API secrets.
* `github_token` - (Required) A user's github repository token. This is used to setup the Github Actions workflow file and API secrets.

* `repo_url` - (Required) URL for the repository of the Static Web App site.
* `github_repo_url` - (Required) URL for the repository of the Static Web App site.

* `api_directory` - (Optional) The path to the Function App api code within the repository.

* `api_location` - (Optional) The path to the Function App api code within the repository.
* `artifact_directory` - (Optional) The path of the Static Web App artifacts after building.

* `tags` - (Optional) A mapping of tags which should be assigned to the Static Web App.

* `artifact_location` - (Optional) The path of the Static Web App artifacts after building.

## Attributes Reference

Expand Down

0 comments on commit a3fba41

Please sign in to comment.