Skip to content

Commit

Permalink
Add dataproc iam resources (#3632)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and emilymye committed May 16, 2019
1 parent 6e4683a commit 3b29005
Show file tree
Hide file tree
Showing 9 changed files with 967 additions and 12 deletions.
116 changes: 116 additions & 0 deletions google/iam_dataproc_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package google

import (
"fmt"

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

var IamDataprocClusterSchema = map[string]*schema.Schema{
"cluster": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
}

type DataprocClusterIamUpdater struct {
project string
region string
cluster string
Config *Config
}

func NewDataprocClusterUpdater(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) {
project, err := getProject(d, config)
if err != nil {
return nil, err
}

region, err := getRegion(d, config)
if err != nil {
return nil, err
}

d.Set("project", project)
d.Set("region", region)

return &DataprocClusterIamUpdater{
project: project,
region: region,
cluster: d.Get("cluster").(string),
Config: config,
}, nil
}

func DataprocClusterIdParseFunc(d *schema.ResourceData, config *Config) error {
fv, err := parseRegionalFieldValue("clusters", d.Id(), "project", "region", "zone", d, config, true)
if err != nil {
return err
}

d.Set("project", fv.Project)
d.Set("region", fv.Region)
d.Set("cluster", fv.Name)

// Explicitly set the id so imported resources have the same ID format as non-imported ones.
d.SetId(fv.RelativeLink())
return nil
}

func (u *DataprocClusterIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
req := &dataproc.GetIamPolicyRequest{}
p, err := u.Config.clientDataproc.Projects.Regions.Clusters.GetIamPolicy(u.GetResourceId(), req).Do()
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err)
}

cloudResourcePolicy, err := dataprocToResourceManagerPolicy(p)
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Invalid IAM policy for %s: {{err}}", u.DescribeResource()), err)
}

return cloudResourcePolicy, nil
}

func (u *DataprocClusterIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error {
dataprocPolicy, err := resourceManagerToDataprocPolicy(policy)
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Invalid IAM policy for %s: {{err}}", u.DescribeResource()), err)
}

req := &dataproc.SetIamPolicyRequest{Policy: dataprocPolicy}
_, err = u.Config.clientDataproc.Projects.Regions.Clusters.SetIamPolicy(u.GetResourceId(), req).Do()
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err)
}

return nil
}

func (u *DataprocClusterIamUpdater) GetResourceId() string {
return fmt.Sprintf("projects/%s/regions/%s/clusters/%s", u.project, u.region, u.cluster)
}

func (u *DataprocClusterIamUpdater) GetMutexKey() string {
return fmt.Sprintf("iam-dataproc-cluster-%s-%s-%s", u.project, u.region, u.cluster)
}

func (u *DataprocClusterIamUpdater) DescribeResource() string {
return fmt.Sprintf("Dataproc Cluster %s/%s/%s", u.project, u.region, u.cluster)
}
134 changes: 134 additions & 0 deletions google/iam_dataproc_job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package google

import (
"fmt"

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

var IamDataprocJobSchema = map[string]*schema.Schema{
"job_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
}

type DataprocJobIamUpdater struct {
project string
region string
jobId string
Config *Config
}

func NewDataprocJobUpdater(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) {
project, err := getProject(d, config)
if err != nil {
return nil, err
}

region, err := getRegion(d, config)
if err != nil {
return nil, err
}

d.Set("project", project)
d.Set("region", region)

return &DataprocJobIamUpdater{
project: project,
region: region,
jobId: d.Get("job_id").(string),
Config: config,
}, nil
}

func DataprocJobIdParseFunc(d *schema.ResourceData, config *Config) error {
fv, err := parseRegionalFieldValue("jobs", d.Id(), "project", "region", "zone", d, config, true)
if err != nil {
return err
}

d.Set("job_id", fv.Name)
d.Set("project", fv.Project)
d.Set("region", fv.Region)

// Explicitly set the id so imported resources have the same ID format as non-imported ones.
d.SetId(fv.RelativeLink())
return nil
}

func (u *DataprocJobIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
req := &dataproc.GetIamPolicyRequest{}
p, err := u.Config.clientDataproc.Projects.Regions.Jobs.GetIamPolicy(u.GetResourceId(), req).Do()
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err)
}

cloudResourcePolicy, err := dataprocToResourceManagerPolicy(p)
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Invalid IAM policy for %s: {{err}}", u.DescribeResource()), err)
}

return cloudResourcePolicy, nil
}

func (u *DataprocJobIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error {
dataprocPolicy, err := resourceManagerToDataprocPolicy(policy)
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Invalid IAM policy for %s: {{err}}", u.DescribeResource()), err)
}

req := &dataproc.SetIamPolicyRequest{Policy: dataprocPolicy}
_, err = u.Config.clientDataproc.Projects.Regions.Jobs.SetIamPolicy(u.GetResourceId(), req).Do()
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err)
}

return nil
}

func (u *DataprocJobIamUpdater) GetResourceId() string {
return fmt.Sprintf("projects/%s/regions/%s/jobs/%s", u.project, u.region, u.jobId)
}

func (u *DataprocJobIamUpdater) GetMutexKey() string {
return fmt.Sprintf("iam-dataproc-job-%s-%s-%s", u.project, u.region, u.jobId)
}

func (u *DataprocJobIamUpdater) DescribeResource() string {
return fmt.Sprintf("Dataproc Job %s/%s/%s", u.project, u.region, u.jobId)
}

func resourceManagerToDataprocPolicy(p *cloudresourcemanager.Policy) (*dataproc.Policy, error) {
out := &dataproc.Policy{}
err := Convert(p, out)
if err != nil {
return nil, errwrap.Wrapf("Cannot convert a dataproc policy to a cloudresourcemanager policy: {{err}}", err)
}
return out, nil
}

func dataprocToResourceManagerPolicy(p *dataproc.Policy) (*cloudresourcemanager.Policy, error) {
out := &cloudresourcemanager.Policy{}
err := Convert(p, out)
if err != nil {
return nil, errwrap.Wrapf("Cannot convert a cloudresourcemanager policy to a dataproc policy: {{err}}", err)
}
return out, nil
}
6 changes: 6 additions & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,13 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_container_node_pool": resourceContainerNodePool(),
"google_dataflow_job": resourceDataflowJob(),
"google_dataproc_cluster": resourceDataprocCluster(),
"google_dataproc_cluster_iam_binding": ResourceIamBindingWithImport(IamDataprocClusterSchema, NewDataprocClusterUpdater, DataprocClusterIdParseFunc),
"google_dataproc_cluster_iam_member": ResourceIamMemberWithImport(IamDataprocClusterSchema, NewDataprocClusterUpdater, DataprocClusterIdParseFunc),
"google_dataproc_cluster_iam_policy": ResourceIamPolicyWithImport(IamDataprocClusterSchema, NewDataprocClusterUpdater, DataprocClusterIdParseFunc),
"google_dataproc_job": resourceDataprocJob(),
"google_dataproc_job_iam_binding": ResourceIamBindingWithImport(IamDataprocJobSchema, NewDataprocJobUpdater, DataprocJobIdParseFunc),
"google_dataproc_job_iam_member": ResourceIamMemberWithImport(IamDataprocJobSchema, NewDataprocJobUpdater, DataprocJobIdParseFunc),
"google_dataproc_job_iam_policy": ResourceIamPolicyWithImport(IamDataprocJobSchema, NewDataprocJobUpdater, DataprocJobIdParseFunc),
"google_dns_record_set": resourceDnsRecordSet(),
"google_endpoints_service": resourceEndpointsService(),
"google_folder": resourceGoogleFolder(),
Expand Down
Loading

0 comments on commit 3b29005

Please sign in to comment.