Skip to content

Commit

Permalink
Move handwritten resources to the service packages (#8147) (#14940)
Browse files Browse the repository at this point in the history
* Move handwritten resources to the service packages

* Fix import path

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jun 20, 2023
1 parent 3b1428c commit f1a8b7c
Show file tree
Hide file tree
Showing 95 changed files with 3,103 additions and 2,858 deletions.
3 changes: 3 additions & 0 deletions .changelog/8147.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:none

```
87 changes: 0 additions & 87 deletions google/data_source_storage_object_signed_url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,103 +5,16 @@ package google
import (
"testing"

"bytes"
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
"net/url"

"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-provider-google/google/acctest"
"golang.org/x/oauth2/google"
)

const fakeCredentials = `{
"type": "service_account",
"project_id": "gcp-project",
"private_key_id": "29a54056cee3d6886d9e8515a959af538ab5add9",
"private_key": "-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAsGHDAdHZfi81LgVeeMHXYLgNDpcFYhoBykYtTDdNyA5AixID\n8JdKlCmZ6qLNnZrbs4JlBJfmzw6rjUC5bVBFg5NwYVBu3+3Msa4rgLsTGsjPH9rt\nC+QFnFhcmzg3zz8eeXBqJdhw7wmn1Xa9SsC3h6YWveBk98ecyE7yGe8J8xGphjk7\nEQ/KBmRK/EJD0ZwuYW1W4Bv5f5fca7qvi9rCprEmL8//uy0qCwoJj2jU3zc5p72M\npkSZb1XlYxxTEo/h9WCEvWS9pGhy6fJ0sA2RsBHqU4Y5O7MJEei9yu5fVSZUi05f\n/ggfUID+cFEq0Z/A98whKPEBBJ/STdEaqEEkBwIDAQABAoIBAED6EsvF0dihbXbh\ntXbI+h4AT5cTXYFRUV2B0sgkC3xqe65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FutxRP\nppI5Q95P12CQ/eRBZKJnRlkhkL8tfPaWPzzOpCTjID7avRhx2oLmstmYuXx0HluE\ncqXLbAV9WDpIJ3Bpa/S8tWujWhLDmixn2JeAdurWS+naH9U9e4I6Rw==\n-----END RSA PRIVATE KEY-----\n",
"client_email": "[email protected]",
"client_id": "103198861025845558729",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/user%40gcp-project.iam.gserviceaccount.com"
}
`

// The following values are derived from the output of the `gsutil signurl` command.
// i.e.
// gsutil signurl fake_creds.json gs://tf-test-bucket-6159205297736845881/path/to/file
// URL HTTP Method Expiration Signed URL
// gs://tf-test-bucket-6159205297736845881/path/to/file GET 2016-08-12 14:03:30 https://storage.googleapis.com/tf-test-bucket-6159205297736845881/path/to/[email protected]&Expires=1470967410&Signature=JJvE2Jc%2BeoagyS1qRACKBGUkgLkKjw7cGymHhtB4IzzN3nbXDqr0acRWGy0%2BEpZ3HYNDalEYsK0lR9Q0WCgty5I0JKmPIuo9hOYa1xTNH%2B22xiWsekxGV%2FcA9FXgWpi%2BFt7fBmMk4dhDe%2BuuYc7N79hd0FYuSBNW1Wp32Bluoe4SNkNAB%2BuIDd9KqPzqs09UAbBoz2y4WxXOQnRyR8GAfb8B%2FDtv62gYjtmp%2F6%2Fyr6xj7byWKZdQt8kEftQLTQmP%2F17Efjp6p%2BXo71Q0F9IhAFiqWfp3Ij8hHDSebLcVb2ULXyHNNQpHBOhFgALrFW3I6Uc3WciLEOsBS9Ej3EGdTg%3D%3D

const testUrlPath = "/tf-test-bucket-6159205297736845881/path/to/file"
const testUrlExpires = 1470967410
const testUrlExpectedSignatureBase64Encoded = "JJvE2Jc%2BeoagyS1qRACKBGUkgLkKjw7cGymHhtB4IzzN3nbXDqr0acRWGy0%2BEpZ3HYNDalEYsK0lR9Q0WCgty5I0JKmPIuo9hOYa1xTNH%2B22xiWsekxGV%2FcA9FXgWpi%2BFt7fBmMk4dhDe%2BuuYc7N79hd0FYuSBNW1Wp32Bluoe4SNkNAB%2BuIDd9KqPzqs09UAbBoz2y4WxXOQnRyR8GAfb8B%2FDtv62gYjtmp%2F6%2Fyr6xj7byWKZdQt8kEftQLTQmP%2F17Efjp6p%2BXo71Q0F9IhAFiqWfp3Ij8hHDSebLcVb2ULXyHNNQpHBOhFgALrFW3I6Uc3WciLEOsBS9Ej3EGdTg%3D%3D"
const testUrlExpectedUrl = "https://storage.googleapis.com/tf-test-bucket-6159205297736845881/path/to/[email protected]&Expires=1470967410&Signature=JJvE2Jc%2BeoagyS1qRACKBGUkgLkKjw7cGymHhtB4IzzN3nbXDqr0acRWGy0%2BEpZ3HYNDalEYsK0lR9Q0WCgty5I0JKmPIuo9hOYa1xTNH%2B22xiWsekxGV%2FcA9FXgWpi%2BFt7fBmMk4dhDe%2BuuYc7N79hd0FYuSBNW1Wp32Bluoe4SNkNAB%2BuIDd9KqPzqs09UAbBoz2y4WxXOQnRyR8GAfb8B%2FDtv62gYjtmp%2F6%2Fyr6xj7byWKZdQt8kEftQLTQmP%2F17Efjp6p%2BXo71Q0F9IhAFiqWfp3Ij8hHDSebLcVb2ULXyHNNQpHBOhFgALrFW3I6Uc3WciLEOsBS9Ej3EGdTg%3D%3D"

func TestUrlData_Signing(t *testing.T) {
urlData := &UrlData{
HttpMethod: "GET",
Expires: testUrlExpires,
Path: testUrlPath,
}
// unescape and decode the expected signature
expectedSig, err := url.QueryUnescape(testUrlExpectedSignatureBase64Encoded)
if err != nil {
t.Error(err)
}
expected, err := base64.StdEncoding.DecodeString(expectedSig)
if err != nil {
t.Error(err)
}

// load fake service account credentials
cfg, err := google.JWTConfigFromJSON([]byte(fakeCredentials), "")
if err != nil {
t.Error(err)
}

// create url data signature
toSign := urlData.SigningString()
result, err := SignString(toSign, cfg)
if err != nil {
t.Error(err)
}

// compare to expected value
if !bytes.Equal(result, expected) {
t.Errorf("Signatures do not match:\n%x\n%x\n", expected, result)
}

}

func TestUrlData_SignedUrl(t *testing.T) {
// load fake service account credentials
cfg, err := google.JWTConfigFromJSON([]byte(fakeCredentials), "")
if err != nil {
t.Error(err)
}

urlData := &UrlData{
HttpMethod: "GET",
Expires: testUrlExpires,
Path: testUrlPath,
JwtConfig: cfg,
}
result, err := urlData.SignedUrl()
if err != nil {
t.Errorf("Could not generated signed url: %+v", err)
}
if result != testUrlExpectedUrl {
t.Errorf("URL does not match expected value:\n%s\n%s", testUrlExpectedUrl, result)
}
}

func TestAccStorageSignedUrl_basic(t *testing.T) {
t.Parallel()

Expand Down
131 changes: 4 additions & 127 deletions google/iam_dataproc_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,142 +3,19 @@
package google

import (
"fmt"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google/google/services/dataproc"
"github.com/hashicorp/terraform-provider-google/google/tpgiamresource"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
"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
d tpgresource.TerraformResourceData
Config *transport_tpg.Config
}
var IamDataprocClusterSchema = dataproc.IamDataprocClusterSchema

func NewDataprocClusterUpdater(d tpgresource.TerraformResourceData, config *transport_tpg.Config) (tpgiamresource.ResourceIamUpdater, error) {
project, err := tpgresource.GetProject(d, config)
if err != nil {
return nil, err
}

region, err := tpgresource.GetRegion(d, config)
if err != nil {
return nil, err
}

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

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

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

if err := d.Set("project", fv.Project); err != nil {
return fmt.Errorf("Error setting project: %s", err)
}
if err := d.Set("region", fv.Region); err != nil {
return fmt.Errorf("Error setting region: %s", err)
}
if err := d.Set("cluster", fv.Name); err != nil {
return fmt.Errorf("Error setting cluster: %s", err)
}

// 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{}

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

p, err := u.Config.NewDataprocClient(userAgent).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)
}

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

req := &dataproc.SetIamPolicyRequest{Policy: dataprocPolicy}
_, err = u.Config.NewDataprocClient(userAgent).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)
return dataproc.DataprocClusterIdParseFunc(d, config)
}
Loading

0 comments on commit f1a8b7c

Please sign in to comment.