From 1acfc5484a6449847f6801b13784622788365615 Mon Sep 17 00:00:00 2001 From: Mikael Gibert Date: Fri, 14 Sep 2018 09:13:28 +0200 Subject: [PATCH] Make runtime configs importable --- google/resource_runtimeconfig_config.go | 18 ++++++++++++++++++ google/resource_runtimeconfig_config_test.go | 5 +++++ google/resource_runtimeconfig_variable.go | 18 ++++++++++++++++++ google/resource_runtimeconfig_variable_test.go | 10 ++++++++++ 4 files changed, 51 insertions(+) diff --git a/google/resource_runtimeconfig_config.go b/google/resource_runtimeconfig_config.go index 35062e9c..151b6c4c 100644 --- a/google/resource_runtimeconfig_config.go +++ b/google/resource_runtimeconfig_config.go @@ -17,6 +17,10 @@ func resourceRuntimeconfigConfig() *schema.Resource { Update: resourceRuntimeconfigConfigUpdate, Delete: resourceRuntimeconfigConfigDelete, + Importer: &schema.ResourceImporter{ + State: resourceRuntimeconfigConfigImport, + }, + Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -131,6 +135,20 @@ func resourceRuntimeconfigConfigDelete(d *schema.ResourceData, meta interface{}) return nil } +func resourceRuntimeconfigConfigImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*Config) + parseImportId([]string{"projects/(?P[^/]+)/configs/(?P[^/]+)", "(?P[^/]+)"}, d, config) + + // Replace import id for the resource id + id, err := replaceVars(d, config, "projects/{{project}}/configs/{{name}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil +} + // resourceRuntimeconfigFullName turns a given project and a 'short name' for a runtime config into a full name // (e.g. projects/my-project/configs/my-config). func resourceRuntimeconfigFullName(project, name string) string { diff --git a/google/resource_runtimeconfig_config_test.go b/google/resource_runtimeconfig_config_test.go index 41be39e9..57ccbdfa 100644 --- a/google/resource_runtimeconfig_config_test.go +++ b/google/resource_runtimeconfig_config_test.go @@ -30,6 +30,11 @@ func TestAccRuntimeconfigConfig_basic(t *testing.T) { testAccCheckRuntimeConfigDescription(&runtimeConfig, description), ), }, + resource.TestStep{ + ResourceName: "google_runtimeconfig_config.foobar", + ImportState: true, + ImportStateVerify: true, + }, }, }) } diff --git a/google/resource_runtimeconfig_variable.go b/google/resource_runtimeconfig_variable.go index 4461829e..6601faa3 100644 --- a/google/resource_runtimeconfig_variable.go +++ b/google/resource_runtimeconfig_variable.go @@ -14,6 +14,10 @@ func resourceRuntimeconfigVariable() *schema.Resource { Update: resourceRuntimeconfigVariableUpdate, Delete: resourceRuntimeconfigVariableDelete, + Importer: &schema.ResourceImporter{ + State: resourceRuntimeconfigVariableImport, + }, + Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -124,6 +128,20 @@ func resourceRuntimeconfigVariableDelete(d *schema.ResourceData, meta interface{ return nil } +func resourceRuntimeconfigVariableImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*Config) + parseImportId([]string{"projects/(?P[^/]+)/configs/(?P[^/]+)/variables/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)"}, d, config) + + // Replace import id for the resource id + id, err := replaceVars(d, config, "projects/{{project}}/configs/{{parent}}/variables/{{name}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil +} + // resourceRuntimeconfigVariableFullName turns a given project, runtime config name, and a 'short name' for a runtime // config variable into a full name (e.g. projects/my-project/configs/my-config/variables/my-variable). func resourceRuntimeconfigVariableFullName(project, config, name string) string { diff --git a/google/resource_runtimeconfig_variable_test.go b/google/resource_runtimeconfig_variable_test.go index f273ba86..b2e83e27 100644 --- a/google/resource_runtimeconfig_variable_test.go +++ b/google/resource_runtimeconfig_variable_test.go @@ -34,6 +34,11 @@ func TestAccRuntimeconfigVariable_basic(t *testing.T) { testAccCheckRuntimeconfigVariableUpdateTime("google_runtimeconfig_variable.foobar"), ), }, + resource.TestStep{ + ResourceName: "google_runtimeconfig_variable.foobar", + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -94,6 +99,11 @@ func TestAccRuntimeconfigVariable_basicValue(t *testing.T) { testAccCheckRuntimeconfigVariableUpdateTime("google_runtimeconfig_variable.foobar"), ), }, + resource.TestStep{ + ResourceName: "google_runtimeconfig_variable.foobar", + ImportState: true, + ImportStateVerify: true, + }, }, }) }