Skip to content
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

Misc improvements for Certificate Authority #2883

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changelog/4407.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
```release-note:new-resource
google_privateca_certificate_authority_iam_policy
```
```release-note:new-resource
google_privateca_certificate_authority_iam_binding
```
```release-note:new-resource
google_privateca_certificate_authority_iam_member
```
166 changes: 166 additions & 0 deletions google-beta/iam_privateca_certificate_authority.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------
package google

import (
"fmt"

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

var PrivatecaCertificateAuthorityIamSchema = map[string]*schema.Schema{
"certificate_authority": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
}

type PrivatecaCertificateAuthorityIamUpdater struct {
certificateAuthority string
d *schema.ResourceData
Config *Config
}

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

if v, ok := d.GetOk("certificate_authority"); ok {
values["certificate_authority"] = v.(string)
}

// We may have gotten either a long or short name, so attempt to parse long name if possible
m, err := getImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/certificateAuthorities/(?P<certificate_authority_id>[^/]+)", "(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<certificate_authority_id>[^/]+)", "(?P<location>[^/]+)/(?P<certificate_authority_id>[^/]+)"}, d, config, d.Get("certificate_authority").(string))
if err != nil {
return nil, err
}

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

u := &PrivatecaCertificateAuthorityIamUpdater{
certificateAuthority: values["certificate_authority"],
d: d,
Config: config,
}

if err := d.Set("certificate_authority", u.GetResourceId()); err != nil {
return nil, fmt.Errorf("Error setting certificate_authority: %s", err)
}

return u, nil
}

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

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

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

u := &PrivatecaCertificateAuthorityIamUpdater{
certificateAuthority: values["certificate_authority"],
d: d,
Config: config,
}
if err := d.Set("certificate_authority", u.GetResourceId()); err != nil {
return fmt.Errorf("Error setting certificate_authority: %s", err)
}
d.SetId(u.GetResourceId())
return nil
}

func (u *PrivatecaCertificateAuthorityIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
url, err := u.qualifyCertificateAuthorityUrl("getIamPolicy")
if err != nil {
return nil, err
}

var obj map[string]interface{}

userAgent, err := generateUserAgentString(u.d, u.Config.userAgent)
if err != nil {
return nil, err
}

policy, err := sendRequest(u.Config, "GET", "", url, userAgent, obj)
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 *PrivatecaCertificateAuthorityIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error {
json, err := ConvertToMap(policy)
if err != nil {
return err
}

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

url, err := u.qualifyCertificateAuthorityUrl("setIamPolicy")
if err != nil {
return err
}

userAgent, err := generateUserAgentString(u.d, u.Config.userAgent)
if err != nil {
return err
}

_, err = sendRequestWithTimeout(u.Config, "POST", "", url, userAgent, 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 (u *PrivatecaCertificateAuthorityIamUpdater) qualifyCertificateAuthorityUrl(methodIdentifier string) (string, error) {
urlTemplate := fmt.Sprintf("{{PrivatecaBasePath}}%s:%s", fmt.Sprintf("%s", u.certificateAuthority), methodIdentifier)
url, err := replaceVars(u.d, u.Config, urlTemplate)
if err != nil {
return "", err
}
return url, nil
}

func (u *PrivatecaCertificateAuthorityIamUpdater) GetResourceId() string {
return fmt.Sprintf("%s", u.certificateAuthority)
}

func (u *PrivatecaCertificateAuthorityIamUpdater) GetMutexKey() string {
return fmt.Sprintf("iam-privateca-certificateauthority-%s", u.GetResourceId())
}

func (u *PrivatecaCertificateAuthorityIamUpdater) DescribeResource() string {
return fmt.Sprintf("privateca certificateauthority %q", u.GetResourceId())
}
Loading