Skip to content

Commit

Permalink
App: internal ports array attribute (#570)
Browse files Browse the repository at this point in the history
* wip

* wip internal_port

* wip internalPort

* internal_port

* internal port

* internal port acc test

* internal_port

* internal_port

* unneeded

* fixup acceptance test

* acceptance test

* Remove unused appSpecInternalPortSchema()

Co-authored-by: Matthieu Berthomé <matthieu@whisbee.eu>
Co-authored-by: Andrew Starr-Bochicchio <a.starr.b@gmail.com>
  • Loading branch information
3 people authored Feb 3, 2021
1 parent 9d98694 commit 2ae1b75
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
31 changes: 31 additions & 0 deletions digitalocean/app_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ func appSpecServicesSchema() *schema.Resource {
Schema: appSpecRouteSchema(),
},
},
"internal_ports": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeInt},
},
}

for k, v := range appSpecComponentBase() {
Expand Down Expand Up @@ -890,6 +895,16 @@ func flattenAppHealthCheck(check *godo.AppServiceSpecHealthCheck) []interface{}
return result
}

func expandAppInternalPorts(config []interface{}) []int64 {
appInternalPorts := make([]int64, len(config))

for i, v := range config {
appInternalPorts[i] = int64(v.(int))
}

return appInternalPorts
}

func expandAppRoutes(config []interface{}) []*godo.AppRouteSpec {
appRoutes := make([]*godo.AppRouteSpec, 0, len(config))

Expand All @@ -906,6 +921,16 @@ func expandAppRoutes(config []interface{}) []*godo.AppRouteSpec {
return appRoutes
}

func flattenAppServiceInternalPortsSpec(internalPorts []int64) *schema.Set {
result := schema.NewSet(schema.HashInt, []interface{}{})

for _, internalPort := range internalPorts {
result.Add(int(internalPort))
}

return result
}

func flattenAppRoutes(routes []*godo.AppRouteSpec) []interface{} {
result := make([]interface{}, 0)

Expand Down Expand Up @@ -968,6 +993,11 @@ func expandAppSpecServices(config []interface{}) []*godo.AppServiceSpec {
s.HealthCheck = expandAppHealthCheck(checks)
}

internalPorts := service["internal_ports"].(*schema.Set).List()
if len(internalPorts) > 0 {
s.InternalPorts = expandAppInternalPorts(internalPorts)
}

appServices = append(appServices, s)
}

Expand All @@ -985,6 +1015,7 @@ func flattenAppSpecServices(services []*godo.AppServiceSpec) []map[string]interf
r["build_command"] = s.BuildCommand
r["github"] = flattenAppGitHubSourceSpec(s.GitHub)
r["gitlab"] = flattenAppGitLabSourceSpec(s.GitLab)
r["internal_ports"] = flattenAppServiceInternalPortsSpec(s.InternalPorts)
r["git"] = flattenAppGitSourceSpec(s.Git)
r["image"] = flattenAppImageSourceSpec(s.Image)
r["http_port"] = int(s.HTTPPort)
Expand Down
59 changes: 59 additions & 0 deletions digitalocean/resource_digitalocean_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,43 @@ func TestAccDigitalOceanApp_StaticSite(t *testing.T) {
})
}

func TestAccDigitalOceanApp_InternalPort(t *testing.T) {
var app godo.App
appName := randomTestName()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDigitalOceanAppDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccCheckDigitalOceanAppConfig_addInternalPort, appName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanAppExists("digitalocean_app.foobar", &app),
resource.TestCheckResourceAttr(
"digitalocean_app.foobar", "spec.0.name", appName),
resource.TestCheckResourceAttrSet("digitalocean_app.foobar", "active_deployment_id"),
resource.TestCheckResourceAttrSet("digitalocean_app.foobar", "updated_at"),
resource.TestCheckResourceAttrSet("digitalocean_app.foobar", "created_at"),
resource.TestCheckResourceAttr(
"digitalocean_app.foobar", "spec.0.service.0.instance_count", "1"),
resource.TestCheckResourceAttr(
"digitalocean_app.foobar", "spec.0.service.0.instance_size_slug", "basic-xxs"),
resource.TestCheckResourceAttr(
"digitalocean_app.foobar", "spec.0.service.0.git.0.repo_clone_url",
"https://github.com/digitalocean/sample-golang.git"),
resource.TestCheckResourceAttr(
"digitalocean_app.foobar", "spec.0.service.0.git.0.branch", "main"),
resource.TestCheckResourceAttr(
"digitalocean_app.foobar", "spec.0.service.0.internal_ports.#", "1"),
resource.TestCheckResourceAttr(
"digitalocean_app.foobar", "spec.0.service.0.internal_ports.0", "5000"),
),
},
},
})
}

func TestAccDigitalOceanApp_Envs(t *testing.T) {
var app godo.App
appName := randomTestName()
Expand Down Expand Up @@ -682,6 +719,28 @@ resource "digitalocean_app" "foobar" {
}
}`

var testAccCheckDigitalOceanAppConfig_addInternalPort = `
resource "digitalocean_app" "foobar" {
spec {
name = "%s"
region = "ams"
service {
name = "go-service"
environment_slug = "go"
instance_count = 1
instance_size_slug = "basic-xxs"
git {
repo_clone_url = "https://github.com/digitalocean/sample-golang.git"
branch = "main"
}
internal_ports = [ 5000 ]
}
}
}`

var testAccCheckDigitalOceanAppConfig_addDatabase = `
resource "digitalocean_app" "foobar" {
spec {
Expand Down

0 comments on commit 2ae1b75

Please sign in to comment.