From 2ae1b75a1adc5b0caf44f93127b6f909f2644cdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthieu=20Berthom=C3=A9?= Date: Wed, 3 Feb 2021 17:33:42 +0100 Subject: [PATCH] App: internal ports array attribute (#570) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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é Co-authored-by: Andrew Starr-Bochicchio --- digitalocean/app_spec.go | 31 ++++++++++ .../resource_digitalocean_app_test.go | 59 +++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/digitalocean/app_spec.go b/digitalocean/app_spec.go index dfafb2f5a..a8f68fc75 100644 --- a/digitalocean/app_spec.go +++ b/digitalocean/app_spec.go @@ -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() { @@ -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)) @@ -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) @@ -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) } @@ -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) diff --git a/digitalocean/resource_digitalocean_app_test.go b/digitalocean/resource_digitalocean_app_test.go index 264e367bd..2741a7adb 100644 --- a/digitalocean/resource_digitalocean_app_test.go +++ b/digitalocean/resource_digitalocean_app_test.go @@ -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() @@ -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 {