Skip to content

Commit

Permalink
Terraform generate iam
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
slevenick authored and modular-magician committed Jul 3, 2019
1 parent b0f7ff1 commit cb0b9a2
Show file tree
Hide file tree
Showing 13 changed files with 896 additions and 407 deletions.
20 changes: 20 additions & 0 deletions google/iam_pubsub_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package google

import (
"fmt"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/cloudresourcemanager/v1"
Expand Down Expand Up @@ -90,3 +91,22 @@ func (u *PubsubSubscriptionIamUpdater) GetMutexKey() string {
func (u *PubsubSubscriptionIamUpdater) DescribeResource() string {
return fmt.Sprintf("pubsub subscription %q", u.subscription)
}

// v1 and v2beta policy are identical
func resourceManagerToPubsubPolicy(in *cloudresourcemanager.Policy) (*pubsub.Policy, error) {
out := &pubsub.Policy{}
err := Convert(in, out)
if err != nil {
return nil, errwrap.Wrapf("Cannot convert a v1 policy to a pubsub policy: {{err}}", err)
}
return out, nil
}

func pubsubToResourceManagerPolicy(in *pubsub.Policy) (*cloudresourcemanager.Policy, error) {
out := &cloudresourcemanager.Policy{}
err := Convert(in, out)
if err != nil {
return nil, errwrap.Wrapf("Cannot convert a pubsub policy to a v1 policy: {{err}}", err)
}
return out, nil
}
112 changes: 0 additions & 112 deletions google/iam_pubsub_topic.go

This file was deleted.

73 changes: 73 additions & 0 deletions google/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,76 @@ func setDefaultValues(idRegex string, d TerraformResourceData, config *Config) e
}
return nil
}

// Parse an import id extracting field values using the given list of regexes.
// They are applied in order. The first in the list is tried first.
// This does not mutate any of the parameters, returning a map of matches
//
// e.g:
// - projects/(?P<project>[^/]+)/regions/(?P<region>[^/]+)/subnetworks/(?P<name>[^/]+) (applied first)
// - (?P<project>[^/]+)/(?P<region>[^/]+)/(?P<name>[^/]+),
// - (?P<name>[^/]+) (applied last)
func getImportIdQualifiers(idRegexes []string, d TerraformResourceData, config *Config, id string) (map[string]string, error) {
for _, idFormat := range idRegexes {
re, err := regexp.Compile(idFormat)

if err != nil {
log.Printf("[DEBUG] Could not compile %s.", idFormat)
return nil, fmt.Errorf("Import is not supported. Invalid regex formats.")
}

if fieldValues := re.FindStringSubmatch(id); fieldValues != nil {
var result map[string]string
result = make(map[string]string)
log.Printf("[DEBUG] matching ID %s to regex %s.", id, idFormat)
// Starting at index 1, the first match is the full string.
for i := 1; i < len(fieldValues); i++ {
fieldName := re.SubexpNames()[i]
fieldValue := fieldValues[i]
result[fieldName] = fieldValue
}

// The first id format is applied first and contains all the fields.
defaults, err := getDefaultValues(idRegexes[0], d, config)
if err != nil {
return nil, err
}

for k, v := range defaults {
result[k] = v
}

return result, nil
}
}
return nil, fmt.Errorf("Resource id %q doesn't match any of the accepted formats: %v", id, idRegexes)
}

// Returns a set of default values that are contained in a regular expression
// This does not mutate any parameters, instead returning a map of defaults
func getDefaultValues(idRegex string, d TerraformResourceData, config *Config) (map[string]string, error) {
var result map[string]string
result = make(map[string]string)
if _, ok := d.GetOk("project"); !ok && strings.Contains(idRegex, "?P<project>") {
project, err := getProject(d, config)
if err != nil {
return nil, err
}
result["project"] = project
}
if _, ok := d.GetOk("region"); !ok && strings.Contains(idRegex, "?P<region>") {
region, err := getRegion(d, config)
if err != nil {
return nil, err
}
result["region"] = region
}
if _, ok := d.GetOk("zone"); !ok && strings.Contains(idRegex, "?P<zone>") {
zone, err := getZone(d, config)
if err != nil {
return nil, err
}
result["zone"] = zone
}
return result, nil
}
3 changes: 0 additions & 3 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,6 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_project_organization_policy": resourceGoogleProjectOrganizationPolicy(),
"google_project_usage_export_bucket": resourceProjectUsageBucket(),
"google_project_services": resourceGoogleProjectServices(),
"google_pubsub_topic_iam_binding": ResourceIamBindingWithImport(IamPubsubTopicSchema, NewPubsubTopicIamUpdater, PubsubTopicIdParseFunc),
"google_pubsub_topic_iam_member": ResourceIamMemberWithImport(IamPubsubTopicSchema, NewPubsubTopicIamUpdater, PubsubTopicIdParseFunc),
"google_pubsub_topic_iam_policy": ResourceIamPolicyWithImport(IamPubsubTopicSchema, NewPubsubTopicIamUpdater, PubsubTopicIdParseFunc),
"google_pubsub_subscription_iam_binding": ResourceIamBindingWithImport(IamPubsubSubscriptionSchema, NewPubsubSubscriptionIamUpdater, PubsubSubscriptionIdParseFunc),
"google_pubsub_subscription_iam_member": ResourceIamMemberWithImport(IamPubsubSubscriptionSchema, NewPubsubSubscriptionIamUpdater, PubsubSubscriptionIdParseFunc),
"google_pubsub_subscription_iam_policy": ResourceIamPolicyWithImport(IamPubsubSubscriptionSchema, NewPubsubSubscriptionIamUpdater, PubsubSubscriptionIdParseFunc),
Expand Down
7 changes: 5 additions & 2 deletions google/provider_pubsub_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ var PubsubCustomEndpointEntry = &schema.Schema{
}

var GeneratedPubsubResourcesMap = map[string]*schema.Resource{
"google_pubsub_topic": resourcePubsubTopic(),
"google_pubsub_subscription": resourcePubsubSubscription(),
"google_pubsub_topic": resourcePubsubTopic(),
"google_pubsub_topic_iam_binding": ResourceIamBindingWithImport(PubsubTopicIamSchema, PubsubTopicIamUpdaterProducer, PubsubTopicIdParseFunc),
"google_pubsub_topic_iam_member": ResourceIamMemberWithImport(PubsubTopicIamSchema, PubsubTopicIamUpdaterProducer, PubsubTopicIdParseFunc),
"google_pubsub_topic_iam_policy": ResourceIamPolicyWithImport(PubsubTopicIamSchema, PubsubTopicIamUpdaterProducer, PubsubTopicIdParseFunc),
"google_pubsub_subscription": resourcePubsubSubscription(),
}
5 changes: 4 additions & 1 deletion google/provider_source_repo_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ var SourceRepoCustomEndpointEntry = &schema.Schema{
}

var GeneratedSourceRepoResourcesMap = map[string]*schema.Resource{
"google_sourcerepo_repository": resourceSourceRepoRepository(),
"google_sourcerepo_repository": resourceSourceRepoRepository(),
"google_sourcerepo_repository_iam_binding": ResourceIamBindingWithImport(SourceRepoRepositoryIamSchema, SourceRepoRepositoryIamUpdaterProducer, SourceRepoRepositoryIdParseFunc),
"google_sourcerepo_repository_iam_member": ResourceIamMemberWithImport(SourceRepoRepositoryIamSchema, SourceRepoRepositoryIamUpdaterProducer, SourceRepoRepositoryIdParseFunc),
"google_sourcerepo_repository_iam_policy": ResourceIamPolicyWithImport(SourceRepoRepositoryIamSchema, SourceRepoRepositoryIamUpdaterProducer, SourceRepoRepositoryIdParseFunc),
}
143 changes: 143 additions & 0 deletions google/resource_pubsub_topic_iam_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package google

import (
"fmt"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/cloudresourcemanager/v1"
)

var PubsubTopicIamSchema = map[string]*schema.Schema{
"project": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
},
"topic": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
}

type PubsubTopicIamUpdater struct {
project string
topic string
d *schema.ResourceData
Config *Config
}

func PubsubTopicIamUpdaterProducer(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) {
values := make(map[string]string)

project, err := getProject(d, config)
if err != nil {
return nil, err
}

values["project"] = project

m, err := getImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/topics/(?P<topic>[^/]+)", "(?P<project>[^/]+)/(?P<topic>[^/]+)", "(?P<topic>[^/]+)"}, d, config, d.Get("topic").(string))
if err != nil {
return nil, err
}

for k, v := range m {
values[k] = v
}

u := &PubsubTopicIamUpdater{
project: values["project"],
topic: values["topic"],
d: d,
Config: config,
}
d.SetId(u.GetResourceId())

return u, nil
}

func PubsubTopicIdParseFunc(d *schema.ResourceData, config *Config) error {
values := make(map[string]string)

project, err := getProject(d, config)
if err != nil {
return err
}

values["project"] = project

m, err := getImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/topics/(?P<topic>[^/]+)", "(?P<project>[^/]+)/(?P<topic>[^/]+)", "(?P<topic>[^/]+)"}, d, config, d.Id())
if err != nil {
return err
}

for k, v := range m {
values[k] = v
}

u := &PubsubTopicIamUpdater{
project: values["project"],
topic: values["topic"],
d: d,
Config: config,
}
d.Set("topic", u.GetResourceId())
d.SetId(u.GetResourceId())
return nil
}

func (u *PubsubTopicIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
url := qualifyTopicUrl(u, "getIamPolicy")

policy, err := sendRequest(u.Config, "GET", url, nil)
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err)
}

out := &cloudresourcemanager.Policy{}
err = Convert(policy, out)
if err != nil {
return nil, errwrap.Wrapf("Cannot convert a policy to a resource manager policy: {{err}}", err)
}

return out, nil
}

func (u *PubsubTopicIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error {
json, err := ConvertToMap(policy)
if err != nil {
return err
}

obj := make(map[string]interface{})
obj["policy"] = json

url := qualifyTopicUrl(u, "setIamPolicy")

_, err = sendRequestWithTimeout(u.Config, "POST", url, obj, u.d.Timeout(schema.TimeoutCreate))
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err)
}

return nil
}

func qualifyTopicUrl(u *PubsubTopicIamUpdater, methodIdentifier string) string {
return fmt.Sprintf("https://pubsub.googleapis.com/v1/%s:%s", fmt.Sprintf("projects/%s/topics/%s", u.project, u.topic), methodIdentifier)
}

func (u *PubsubTopicIamUpdater) GetResourceId() string {
return fmt.Sprintf("projects/%s/topics/%s", u.project, u.topic)
}

func (u *PubsubTopicIamUpdater) GetMutexKey() string {
return fmt.Sprintf("iam-pubsub-topic-%s", u.GetResourceId())
}

func (u *PubsubTopicIamUpdater) DescribeResource() string {
return fmt.Sprintf("pubsub topic %q", u.GetResourceId())
}
Loading

0 comments on commit cb0b9a2

Please sign in to comment.