forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
google_service_account_key
to TGC (GoogleCloudPlatf…
- Loading branch information
Showing
5 changed files
with
270 additions
and
1 deletion.
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,208 @@ | ||
package google | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/tfplan2cai/converters/google/resources/cai" | ||
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" | ||
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" | ||
) | ||
|
||
const ServiceAccountKeyAssetType string = "iam.googleapis.com/ServiceAccountKey" | ||
|
||
func resourceConverterServiceAccountKey() cai.ResourceConverter { | ||
return cai.ResourceConverter{ | ||
AssetType: ServiceAccountKeyAssetType, | ||
Convert: GetServiceAccountKeyCaiObject, | ||
} | ||
} | ||
|
||
func GetServiceAccountKeyCaiObject(d tpgresource.TerraformResourceData, config *transport_tpg.Config) ([]cai.Asset, error) { | ||
name, err := cai.AssetName(d, config, "//iam.googleapis.com/projects/{{project}}/serviceAccounts/{{account}}/keys/{{key}}") | ||
if err != nil { | ||
return []cai.Asset{}, err | ||
} | ||
if obj, err := GetServiceAccountKeyApiObject(d, config); err == nil { | ||
return []cai.Asset{{ | ||
Name: name, | ||
Type: ServiceAccountKeyAssetType, | ||
Resource: &cai.AssetResource{ | ||
Version: "v1", | ||
DiscoveryDocumentURI: "https://iam.googleapis.com/$discovery/rest?version=v1", | ||
DiscoveryName: "ServiceAccountKey", | ||
Data: obj, | ||
}, | ||
}}, nil | ||
} else { | ||
return []cai.Asset{}, err | ||
} | ||
} | ||
|
||
func GetServiceAccountKeyApiObject(d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]interface{}, error) { | ||
obj := make(map[string]interface{}) | ||
|
||
idProp, err := expandServiceAccountKeyId(d.Get("id"), d, config) | ||
if err != nil { | ||
return nil, err | ||
} else if v, ok := d.GetOkExists("id"); !tpgresource.IsEmptyValue(reflect.ValueOf(idProp)) && (ok || !reflect.DeepEqual(v, idProp)) { | ||
obj["id"] = idProp | ||
} | ||
|
||
nameProp, err := expandServiceAccountKeyName(d.Get("name"), d, config) | ||
if err != nil { | ||
return nil, err | ||
} else if v, ok := d.GetOkExists("name"); !tpgresource.IsEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) { | ||
obj["name"] = nameProp | ||
} | ||
|
||
privateKeyTypeProp, err := expandServiceAccountKeyPrivateKeyType(d.Get("privateKeyType"), d, config) | ||
if err != nil { | ||
return nil, err | ||
} else if v, ok := d.GetOkExists("privateKeyType"); !tpgresource.IsEmptyValue(reflect.ValueOf(privateKeyTypeProp)) && (ok || !reflect.DeepEqual(v, privateKeyTypeProp)) { | ||
obj["privateKeyType"] = privateKeyTypeProp | ||
} | ||
|
||
keyAlgorithmProp, err := expandServiceAccountKeyKeyAlgorithm(d.Get("keyAlgorithm"), d, config) | ||
if err != nil { | ||
return nil, err | ||
} else if v, ok := d.GetOkExists("keyAlgorithm"); !tpgresource.IsEmptyValue(reflect.ValueOf(keyAlgorithmProp)) && (ok || !reflect.DeepEqual(v, keyAlgorithmProp)) { | ||
obj["keyAlgorithm"] = keyAlgorithmProp | ||
} | ||
|
||
privateKeyDataProp, err := expandServiceAccountKeyKeyPrivateKeyData(d.Get("privateKeyData"), d, config) | ||
if err != nil { | ||
return nil, err | ||
} else if v, ok := d.GetOkExists("privateKeyData"); !tpgresource.IsEmptyValue(reflect.ValueOf(privateKeyDataProp)) && (ok || !reflect.DeepEqual(v, privateKeyDataProp)) { | ||
obj["privateKeyData"] = privateKeyDataProp | ||
} | ||
|
||
publicKeyDataProp, err := expandServiceAccountKeyPublicKeyData(d.Get("publicKeyData"), d, config) | ||
if err != nil { | ||
return nil, err | ||
} else if v, ok := d.GetOkExists("publicKeyData"); !tpgresource.IsEmptyValue(reflect.ValueOf(publicKeyDataProp)) && (ok || !reflect.DeepEqual(v, publicKeyDataProp)) { | ||
obj["publicKeyData"] = publicKeyDataProp | ||
} | ||
|
||
validAfterTimeProp, err := expandServiceAccountKeyValidAfterTime(d.Get("validAfterTime"), d, config) | ||
if err != nil { | ||
return nil, err | ||
} else if v, ok := d.GetOkExists("validAfterTime"); !tpgresource.IsEmptyValue(reflect.ValueOf(validAfterTimeProp)) && (ok || !reflect.DeepEqual(v, validAfterTimeProp)) { | ||
obj["validAfterTime"] = validAfterTimeProp | ||
} | ||
|
||
validBeforeTimeProp, err := expandServiceAccountKeyValidBeforeTime(d.Get("validBeforeTime"), d, config) | ||
if err != nil { | ||
return nil, err | ||
} else if v, ok := d.GetOkExists("validBeforeTime"); !tpgresource.IsEmptyValue(reflect.ValueOf(validBeforeTimeProp)) && (ok || !reflect.DeepEqual(v, validBeforeTimeProp)) { | ||
obj["validBeforeTime"] = validBeforeTimeProp | ||
} | ||
|
||
keyOriginProp, err := expandServiceAccountKeyKeyOrigin(d.Get("keyOrigin"), d, config) | ||
if err != nil { | ||
return nil, err | ||
} else if v, ok := d.GetOkExists("keyOrigin"); !tpgresource.IsEmptyValue(reflect.ValueOf(keyOriginProp)) && (ok || !reflect.DeepEqual(v, keyOriginProp)) { | ||
obj["keyOrigin"] = keyOriginProp | ||
} | ||
|
||
keyTypeProp, err := expandServiceAccountKeykeyType(d.Get("keyType"), d, config) | ||
if err != nil { | ||
return nil, err | ||
} else if v, ok := d.GetOkExists("keyType"); !tpgresource.IsEmptyValue(reflect.ValueOf(keyTypeProp)) && (ok || !reflect.DeepEqual(v, keyTypeProp)) { | ||
obj["keyType"] = keyTypeProp | ||
} | ||
|
||
disabledProp, err := expandServiceAccountKeyDisabled(d.Get("keyType"), d, config) | ||
if err != nil { | ||
return nil, err | ||
} else if v, ok := d.GetOkExists("disabled"); !tpgresource.IsEmptyValue(reflect.ValueOf(disabledProp)) && (ok || !reflect.DeepEqual(v, disabledProp)) { | ||
obj["disabled"] = disabledProp | ||
} | ||
|
||
disableReasonProp, err := expandServiceAccountKeyDisableReason(d.Get("keyType"), d, config) | ||
if err != nil { | ||
return nil, err | ||
} else if v, ok := d.GetOkExists("disableReason"); !tpgresource.IsEmptyValue(reflect.ValueOf(disableReasonProp)) && (ok || !reflect.DeepEqual(v, disableReasonProp)) { | ||
obj["disableReason"] = disableReasonProp | ||
} | ||
|
||
extendedStatusProp, err := expandServiceAccountKeyExtendedStatus(d.Get("extendedStatus"), d, config) | ||
if err != nil { | ||
return nil, err | ||
} else if v, ok := d.GetOkExists("extendedStatus"); !tpgresource.IsEmptyValue(reflect.ValueOf(extendedStatusProp)) && (ok || !reflect.DeepEqual(v, extendedStatusProp)) { | ||
obj["extendedStatus"] = extendedStatusProp | ||
} | ||
|
||
contactProp, err := expandServiceAccountKeyContact(d.Get("contact"), d, config) | ||
if err != nil { | ||
return nil, err | ||
} else if v, ok := d.GetOkExists("contact"); !tpgresource.IsEmptyValue(reflect.ValueOf(contactProp)) && (ok || !reflect.DeepEqual(v, contactProp)) { | ||
obj["contact"] = contactProp | ||
} | ||
|
||
descriptionProp, err := expandServiceAccountKeyDescription(d.Get("description"), d, config) | ||
if err != nil { | ||
return nil, err | ||
} else if v, ok := d.GetOkExists("description"); !tpgresource.IsEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) { | ||
obj["description"] = descriptionProp | ||
} | ||
|
||
creatorProp, err := expandServiceAccountKeyCreator(d.Get("creator"), d, config) | ||
if err != nil { | ||
return nil, err | ||
} else if v, ok := d.GetOkExists("creator"); !tpgresource.IsEmptyValue(reflect.ValueOf(creatorProp)) && (ok || !reflect.DeepEqual(v, creatorProp)) { | ||
obj["creator"] = creatorProp | ||
} | ||
|
||
return obj, nil | ||
} | ||
|
||
func expandServiceAccountKeyId(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
|
||
func expandServiceAccountKeyName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
|
||
func expandServiceAccountKeyPrivateKeyType(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
func expandServiceAccountKeyKeyAlgorithm(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
func expandServiceAccountKeyKeyPrivateKeyData(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
func expandServiceAccountKeyPublicKeyData(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
func expandServiceAccountKeyValidAfterTime(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
func expandServiceAccountKeyValidBeforeTime(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
func expandServiceAccountKeyKeyOrigin(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
func expandServiceAccountKeykeyType(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
func expandServiceAccountKeyDisabled(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
func expandServiceAccountKeyDisableReason(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
func expandServiceAccountKeyExtendedStatus(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
func expandServiceAccountKeyContact(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
func expandServiceAccountKeyDescription(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
func expandServiceAccountKeyCreator(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} |
35 changes: 35 additions & 0 deletions
35
mmv1/third_party/tgc/tests/data/example_service_account_key.json
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,35 @@ | ||
[ | ||
{ | ||
"name":"//iam.googleapis.com/projects/{{.Provider.project}}/serviceAccounts/placeholder-KBbCTuBr/keys/placeholder-kATb9rvt", | ||
"asset_type":"iam.googleapis.com/ServiceAccountKey", | ||
"resource":{ | ||
"version":"v1", | ||
"discovery_document_uri":"https://iam.googleapis.com/$discovery/rest?version=v1", | ||
"discovery_name":"ServiceAccountKey", | ||
"parent":"//cloudresourcemanager.googleapis.com/projects/{{.Provider.project}}", | ||
"data":{ | ||
|
||
} | ||
}, | ||
"ancestry_path": "{{.Ancestry}}/project/{{.Provider.project}}", | ||
"ancestors": ["organizations/{{.OrgID}}"] | ||
|
||
}, | ||
{ | ||
"name":"//iam.googleapis.com/projects/{{.Provider.project}}/serviceAccounts/placeholder-ddCfULac", | ||
"asset_type":"iam.googleapis.com/ServiceAccount", | ||
"resource":{ | ||
"version":"v1", | ||
"discovery_document_uri":"https://iam.googleapis.com/$discovery/rest", | ||
"discovery_name":"ServiceAccount", | ||
"parent":"//cloudresourcemanager.googleapis.com/projects/{{.Provider.project}}", | ||
"data":{ | ||
"displayName":"My Service Account", | ||
"email":"myaccount@{{.Provider.project}}.iam.gserviceaccount.com", | ||
"projectId":"{{.Provider.project}}" | ||
} | ||
}, | ||
"ancestry_path": "{{.Ancestry}}/project/{{.Provider.project}}", | ||
"ancestors": ["organizations/{{.OrgID}}"] | ||
} | ||
] |
22 changes: 22 additions & 0 deletions
22
mmv1/third_party/tgc/tests/data/example_service_account_key.tf
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,22 @@ | ||
terraform { | ||
required_providers { | ||
google = { | ||
source = "hashicorp/google-beta" | ||
version = "~> {{.Provider.version}}" | ||
} | ||
} | ||
} | ||
|
||
provider "google" { | ||
{{if .Provider.credentials }}credentials = "{{.Provider.credentials}}"{{end}} | ||
} | ||
|
||
resource "google_service_account" "myaccount" { | ||
account_id = "myaccount" | ||
display_name = "My Service Account" | ||
} | ||
|
||
resource "google_service_account_key" "mykey" { | ||
service_account_id = google_service_account.myaccount.name | ||
public_key_type = "TYPE_X509_PEM_FILE" | ||
} |