-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Google RuntimeConfig #315
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e86c046
Vendor runtimeconfig
selmanj 0cdf7c7
Add support for RuntimeConfig config and variable resources
selmanj 4dad628
Remove typo
selmanj 069d238
Use top-level declaration rather than init()
selmanj b4219c6
Cleanup testing-related code by using ConflictsWith
selmanj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"google.golang.org/api/runtimeconfig/v1beta1" | ||
) | ||
|
||
var runtimeConfigFullName *regexp.Regexp = regexp.MustCompile("^projects/([^/]+)/configs/(.+)$") | ||
|
||
func resourceRuntimeconfigConfig() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceRuntimeconfigConfigCreate, | ||
Read: resourceRuntimeconfigConfigRead, | ||
Update: resourceRuntimeconfigConfigUpdate, | ||
Delete: resourceRuntimeconfigConfigDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validateGCPName, | ||
}, | ||
|
||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
|
||
"project": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceRuntimeconfigConfigCreate(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
project, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
name := d.Get("name").(string) | ||
fullName := resourceRuntimeconfigFullName(project, name) | ||
runtimeConfig := runtimeconfig.RuntimeConfig{ | ||
Name: fullName, | ||
} | ||
|
||
if val, ok := d.GetOk("description"); ok { | ||
runtimeConfig.Description = val.(string) | ||
} | ||
|
||
_, err = config.clientRuntimeconfig.Projects.Configs.Create("projects/"+project, &runtimeConfig).Do() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No waiting on the operation to complete? Is this synchronous? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is! |
||
|
||
if err != nil { | ||
return err | ||
} | ||
d.SetId(fullName) | ||
|
||
return nil | ||
} | ||
|
||
func resourceRuntimeconfigConfigRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
fullName := d.Id() | ||
runConfig, err := config.clientRuntimeconfig.Projects.Configs.Get(fullName).Do() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
project, name, err := resourceRuntimeconfigParseFullName(runConfig.Name) | ||
if err != nil { | ||
return err | ||
} | ||
// Check to see if project matches our current defined value - if it doesn't, we'll explicitly set it | ||
curProject, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
if project != curProject { | ||
d.Set("project", project) | ||
} | ||
|
||
d.Set("name", name) | ||
d.Set("description", runConfig.Description) | ||
|
||
return nil | ||
} | ||
|
||
func resourceRuntimeconfigConfigUpdate(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
// Update works more like an 'overwrite' method - we build a new runtimeconfig.RuntimeConfig struct and it becomes | ||
// the new config. This means our Update logic looks an awful lot like Create (and hence, doesn't use | ||
// schema.ResourceData.hasChange()). | ||
fullName := d.Id() | ||
runtimeConfig := runtimeconfig.RuntimeConfig{ | ||
Name: fullName, | ||
} | ||
if v, ok := d.GetOk("description"); ok { | ||
runtimeConfig.Description = v.(string) | ||
} | ||
|
||
_, err := config.clientRuntimeconfig.Projects.Configs.Update(fullName, &runtimeConfig).Do() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func resourceRuntimeconfigConfigDelete(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
fullName := d.Id() | ||
|
||
_, err := config.clientRuntimeconfig.Projects.Configs.Delete(fullName).Do() | ||
if err != nil { | ||
return err | ||
} | ||
d.SetId("") | ||
return 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 { | ||
return fmt.Sprintf("projects/%s/configs/%s", project, name) | ||
} | ||
|
||
// resourceRuntimeconfigParseFullName parses a full name (e.g. projects/my-project/configs/my-config) by parsing out the | ||
// project and the short name. Returns "", "", nil upon error. | ||
func resourceRuntimeconfigParseFullName(fullName string) (project, name string, err error) { | ||
matches := runtimeConfigFullName.FindStringSubmatch(fullName) | ||
if matches == nil { | ||
return "", "", fmt.Errorf("Given full name doesn't match expected regexp; fullname = '%s'", fullName) | ||
} | ||
return matches[1], matches[2], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
"google.golang.org/api/runtimeconfig/v1beta1" | ||
) | ||
|
||
func TestAccRuntimeconfigConfig_basic(t *testing.T) { | ||
var runtimeConfig runtimeconfig.RuntimeConfig | ||
configName := fmt.Sprintf("runtimeconfig-test-%s", acctest.RandString(10)) | ||
description := "my test description" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckRuntimeconfigConfigDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccRuntimeconfigConfig_basicDescription(configName, description), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckRuntimeConfigExists( | ||
"google_runtimeconfig_config.foobar", &runtimeConfig), | ||
testAccCheckRuntimeConfigDescription(&runtimeConfig, description), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccRuntimeconfig_update(t *testing.T) { | ||
var runtimeConfig runtimeconfig.RuntimeConfig | ||
configName := fmt.Sprintf("runtimeconfig-test-%s", acctest.RandString(10)) | ||
firstDescription := "my test description" | ||
secondDescription := "my updated test description" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckRuntimeconfigConfigDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccRuntimeconfigConfig_basicDescription(configName, firstDescription), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckRuntimeConfigExists( | ||
"google_runtimeconfig_config.foobar", &runtimeConfig), | ||
testAccCheckRuntimeConfigDescription(&runtimeConfig, firstDescription), | ||
), | ||
}, { | ||
Config: testAccRuntimeconfigConfig_basicDescription(configName, secondDescription), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckRuntimeConfigExists( | ||
"google_runtimeconfig_config.foobar", &runtimeConfig), | ||
testAccCheckRuntimeConfigDescription(&runtimeConfig, secondDescription), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccRuntimeconfig_updateEmptyDescription(t *testing.T) { | ||
var runtimeConfig runtimeconfig.RuntimeConfig | ||
configName := fmt.Sprintf("runtimeconfig-test-%s", acctest.RandString(10)) | ||
description := "my test description" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckRuntimeconfigConfigDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccRuntimeconfigConfig_basicDescription(configName, description), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckRuntimeConfigExists( | ||
"google_runtimeconfig_config.foobar", &runtimeConfig), | ||
testAccCheckRuntimeConfigDescription(&runtimeConfig, description), | ||
), | ||
}, { | ||
Config: testAccRuntimeconfigConfig_emptyDescription(configName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckRuntimeConfigExists( | ||
"google_runtimeconfig_config.foobar", &runtimeConfig), | ||
testAccCheckRuntimeConfigDescription(&runtimeConfig, ""), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckRuntimeConfigDescription(runtimeConfig *runtimeconfig.RuntimeConfig, description string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
if runtimeConfig.Description != description { | ||
return fmt.Errorf("On runtime config '%s', expected description '%s', but found '%s'", | ||
runtimeConfig.Name, description, runtimeConfig.Description) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckRuntimeConfigExists(resourceName string, runtimeConfig *runtimeconfig.RuntimeConfig) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[resourceName] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", resourceName) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No ID is set") | ||
} | ||
|
||
config := testAccProvider.Meta().(*Config) | ||
|
||
found, err := config.clientRuntimeconfig.Projects.Configs.Get(rs.Primary.ID).Do() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*runtimeConfig = *found | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckRuntimeconfigConfigDestroy(s *terraform.State) error { | ||
config := testAccProvider.Meta().(*Config) | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "google_runtimeconfig_config" { | ||
continue | ||
} | ||
|
||
_, err := config.clientRuntimeconfig.Projects.Configs.Get(rs.Primary.ID).Do() | ||
|
||
if err == nil { | ||
return fmt.Errorf("Runtimeconfig still exists") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccRuntimeconfigConfig_basicDescription(name, description string) string { | ||
return fmt.Sprintf(` | ||
resource "google_runtimeconfig_config" "foobar" { | ||
name = "%s" | ||
description = "%s" | ||
}`, name, description) | ||
} | ||
|
||
func testAccRuntimeconfigConfig_emptyDescription(name string) string { | ||
return fmt.Sprintf(` | ||
resource "google_runtimeconfig_config" "foobar" { | ||
name = "%s" | ||
}`, name) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can simply this by removing the if-check and keeping only:
If description is not set,
d.Get("description").(string)
will return the default value""
(empty string).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't do this because I feel like
GetOk
reads better, although I agree they are equivalent.