From 10190a1f4479acbf3891a07ec38e36f9fe4eeca5 Mon Sep 17 00:00:00 2001 From: Dimitrios Karagiannis Date: Wed, 30 Oct 2019 11:29:06 +0000 Subject: [PATCH] Extract resourceAttributePrivatePublic schema Signed-off-by: Dimitrios Karagiannis --- megaport/common.go | 21 +++++++++++++++++++++ megaport/resource_megaport_port.go | 18 +----------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/megaport/common.go b/megaport/common.go index 48f78f2..add4b26 100644 --- a/megaport/common.go +++ b/megaport/common.go @@ -1,10 +1,31 @@ package megaport import ( + "fmt" + "strings" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/utilitywarehouse/terraform-provider-megaport/megaport/api" ) +func resourceAttributePrivatePublic() *schema.Schema { + return &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Default: "private", + StateFunc: func(v interface{}) string { + return strings.ToLower(v.(string)) + }, + ValidateFunc: func(v interface{}, k string) (warns []string, errs []error) { + vv := strings.ToLower(v.(string)) + if vv != "public" && vv != "private" { + errs = append(errs, fmt.Errorf("%s must be either 'public' or 'private', got %s", k, vv)) + } + return + }, + } +} + func resourceMegaportVxcEndElem() *schema.Resource { return &schema.Resource{ Schema: map[string]*schema.Schema{ diff --git a/megaport/resource_megaport_port.go b/megaport/resource_megaport_port.go index 5f8a2c7..14a59a2 100644 --- a/megaport/resource_megaport_port.go +++ b/megaport/resource_megaport_port.go @@ -1,9 +1,7 @@ package megaport import ( - "fmt" "log" - "strings" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/utilitywarehouse/terraform-provider-megaport/megaport/api" @@ -49,21 +47,7 @@ func resourceMegaportPort() *schema.Resource { Elem: resourceMegaportPrivateVxc(), Set: schema.HashResource(resourceMegaportPrivateVxc()), }, - "marketplace_visibility": { - Type: schema.TypeString, - Optional: true, - Default: "private", - StateFunc: func(v interface{}) string { - return strings.ToLower(v.(string)) - }, - ValidateFunc: func(v interface{}, k string) (warns []string, errs []error) { - vv := strings.ToLower(v.(string)) - if vv != "public" && vv != "private" { - errs = append(errs, fmt.Errorf("%s must be either 'public' or 'private', got %s", k, vv)) - } - return - }, - }, + "marketplace_visibility": resourceAttributePrivatePublic(), // TODO: LAG ports }, }