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

Add manual converter and tests for service_account #7146

Merged
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
2 changes: 2 additions & 0 deletions mmv1/provider/terraform_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ def copy_common_files(output_folder, generate_code, _generate_docs)
'third_party/validator/project_service.go'],
['converters/google/resources/monitoring_slo_helper.go',
'third_party/validator/monitoring_slo_helper.go'],
['converters/google/resources/service_account.go',
'third_party/validator/service_account.go'],
['converters/google/resources/image.go',
'third_party/terraform/utils/image.go'],
['converters/google/resources/import.go',
Expand Down
1 change: 1 addition & 0 deletions mmv1/templates/validator/resource_converters.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,6 @@ func ResourceConverters() map[string][]ResourceConverter {
"google_organization_iam_custom_role": {resourceConverterOrganizationIAMCustomRole()},
"google_vpc_access_connector": {resourceConverterVPCAccessConnector()},
"google_logging_metric": {resourceConverterLoggingMetric()},
"google_service_account": {resourceConverterServiceAccount()},
}
}
138 changes: 138 additions & 0 deletions mmv1/third_party/validator/service_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package google

import (
"fmt"
"reflect"
)

const ServiceAccountAssetType string = "iam.googleapis.com/ServiceAccount"

func resourceConverterServiceAccount() ResourceConverter {
return ResourceConverter{
AssetType: ServiceAccountAssetType,
Convert: GetServiceAccountCaiObject,
}
}

func GetServiceAccountCaiObject(d TerraformResourceData, config *Config) ([]Asset, error) {
name, err := assetName(d, config, "//iam.googleapis.com/projects/{{project}}/serviceAccounts/{{unique_id}}")
melinath marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return []Asset{}, err
}
if obj, err := GetServiceAccountApiObject(d, config); err == nil {
return []Asset{{
Name: name,
Type: ServiceAccountAssetType,
Resource: &AssetResource{
Version: "v1",
DiscoveryDocumentURI: "https://iam.googleapis.com/$discovery/rest",
DiscoveryName: "ServiceAccount",
Data: obj,
},
}}, nil
} else {
return []Asset{}, err
}
}

func GetServiceAccountApiObject(d TerraformResourceData, config *Config) (map[string]interface{}, error) {
obj := make(map[string]interface{})

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

descriptionProp, err := expandServiceAccountDescription(d.Get("description"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
obj["description"] = descriptionProp
}

emailProp, err := expandServiceAccountDescription(d.Get("email"), d, config)
melinath marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("email"); !isEmptyValue(reflect.ValueOf(emailProp)) && (ok || !reflect.DeepEqual(v, emailProp)) {
obj["email"] = emailProp
}

displayNameProp, err := expandServiceAccountDisplayName(d.Get("display_name"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("display_name"); !isEmptyValue(reflect.ValueOf(displayNameProp)) && (ok || !reflect.DeepEqual(v, displayNameProp)) {
obj["displayName"] = displayNameProp
}

nameProp, err := expandServiceAccountName(d.Get("name"), d, config)
melinath marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) {
obj["name"] = nameProp
}

disabledProp, err := expandServiceAccountDisabled(d.Get("disabled"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("disabled"); !isEmptyValue(reflect.ValueOf(disabledProp)) && (ok || !reflect.DeepEqual(v, disabledProp)) {
obj["disabled"] = disabledProp
}

uniqueIdProp, err := expandServiceAccountUniqueId(d.Get("unique_id"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("unique_id"); !isEmptyValue(reflect.ValueOf(uniqueIdProp)) && (ok || !reflect.DeepEqual(v, uniqueIdProp)) {
obj["uniqueId"] = uniqueIdProp
}

projectProp, err := expandServiceAccountProject(d.Get("project"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("project"); !isEmptyValue(reflect.ValueOf(projectProp)) && (ok || !reflect.DeepEqual(v, projectProp)) {
obj["projectId"] = projectProp
}

accountIdProp, err := expandServiceAccountId(d.Get("account_id"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("account_id"); !isEmptyValue(reflect.ValueOf(accountIdProp)) && (ok || !reflect.DeepEqual(v, accountIdProp)) {
accountId := accountIdProp
if _, ok := obj["email"]; !ok {
// Generating email when the service account is being created (email not present)
obj["email"] = fmt.Sprintf("%s@%s.iam.gserviceaccount.com", accountId, project)
}
}
return obj, nil
}

func expandServiceAccountId(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandServiceAccountDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandServiceAccountDisplayName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandServiceAccountName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandServiceAccountEmail(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandServiceAccountDisabled(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandServiceAccountUniqueId(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandServiceAccountProject(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,20 @@
}
}
}
},
{
"name": "//iam.googleapis.com/projects/{{.Provider.project}}/serviceAccounts/placeholder-unique-id",
melinath marked this conversation as resolved.
Show resolved Hide resolved
"asset_type": "iam.googleapis.com/ServiceAccount",
"ancestry_path": "{{.Ancestry}}/project/{{.Provider.project}}",
"resource": {
"version": "v1",
"discovery_document_uri": "https://iam.googleapis.com/$discovery/rest",
"discovery_name": "ServiceAccount",
"parent": "//cloudresourcemanager.googleapis.com/projects/{{.Provider.project}}",
"data": {
"displayName": "Service Account",
"email": "service-account-id@{{.Provider.project}}.iam.gserviceaccount.com"
}
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,20 @@
"name": "my-node-pool"
}
}
},
{
"name": "//iam.googleapis.com/projects/{{.Provider.project}}/serviceAccounts/placeholder-unique-id",
"asset_type": "iam.googleapis.com/ServiceAccount",
"ancestry_path": "{{.Ancestry}}/project/{{.Provider.project}}",
"resource": {
"version": "v1",
"discovery_document_uri": "https://iam.googleapis.com/$discovery/rest",
"discovery_name": "ServiceAccount",
"parent": "//cloudresourcemanager.googleapis.com/projects/{{.Provider.project}}",
"data": {
"displayName": "Service Account",
"email": "service-account-id@{{.Provider.project}}.iam.gserviceaccount.com"
}
}
}
]
18 changes: 18 additions & 0 deletions mmv1/third_party/validator/tests/data/example_service_account.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"name": "//iam.googleapis.com/projects/{{.Provider.project}}/serviceAccounts/placeholder-unique-id",
"asset_type": "iam.googleapis.com/ServiceAccount",
"ancestry_path": "{{.Ancestry}}/project/{{.Provider.project}}",
"resource": {
"version": "v1",
"discovery_document_uri": "https://iam.googleapis.com/$discovery/rest",
"discovery_name": "ServiceAccount",
"parent": "//cloudresourcemanager.googleapis.com/projects/{{.Provider.project}}",
"data": {
"displayName": "Service Account",
"email": "service-account-id@{{.Provider.project}}.iam.gserviceaccount.com",
"description": "Service Account Description"
}
}
}
]
35 changes: 35 additions & 0 deletions mmv1/third_party/validator/tests/data/example_service_account.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> {{.Provider.version}}"
}
}
}

provider "google" {
{{if .Provider.credentials }}credentials = "{{.Provider.credentials}}"{{end}}
}

resource "google_service_account" "service_account" {
account_id = "service-account-id"
display_name = "Service Account"
description = "Service Account Description"
disabled = false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"format_version": "1.1",
"terraform_version": "1.3.6",
"planned_values": {
"root_module": {
"resources": [
{
"address": "google_service_account.service_account",
"mode": "managed",
"type": "google_service_account",
"name": "service_account",
"provider_name": "registry.terraform.io/hashicorp/google",
"schema_version": 0,
"values": {
"account_id": "service-account-id",
"description": "Service Account Description",
"disabled": false,
"display_name": "Service Account",
"timeouts": null
},
"sensitive_values": {}
}
]
}
},
"resource_changes": [
{
"address": "google_service_account.service_account",
"mode": "managed",
"type": "google_service_account",
"name": "service_account",
"provider_name": "registry.terraform.io/hashicorp/google",
"change": {
"actions": [
"create"
],
"before": null,
"after": {
"account_id": "service-account-id",
"description": "Service Account Description",
"disabled": false,
"display_name": "Service Account",
"timeouts": null
},
"after_unknown": {
"email": true,
"id": true,
"member": true,
"name": true,
"project": true,
"unique_id": true
},
"before_sensitive": false,
"after_sensitive": {}
}
}
],
"configuration": {
"provider_config": {
"google": {
"name": "google",
"full_name": "registry.terraform.io/hashicorp/google",
"expressions": {
"project": {
"constant_value": "{{.Provider.project}}"
},
"region": {
"constant_value": "asia-southeast-1"
}
}
}
},
"root_module": {
"resources": [
{
"address": "google_service_account.service_account",
"mode": "managed",
"type": "google_service_account",
"name": "service_account",
"provider_config_key": "google",
"expressions": {
"account_id": {
"constant_value": "service-account-id"
},
"description": {
"constant_value": "Service Account Description"
},
"disabled": {
"constant_value": false
},
"display_name": {
"constant_value": "Service Account"
}
},
"schema_version": 0
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[
{
"name": "//iam.googleapis.com/projects/{{.Provider.project}}/serviceAccounts/108592438577779299646",
"asset_type": "iam.googleapis.com/ServiceAccount",
"ancestry_path": "{{.Ancestry}}/project/{{.Provider.project}}",
"resource": {
"version": "v1",
"discovery_document_uri": "https://iam.googleapis.com/$discovery/rest",
"discovery_name": "ServiceAccount",
"parent": "//cloudresourcemanager.googleapis.com/projects/{{.Provider.project}}",
"data": {
"displayName": "Service Account",
"email": "service-account-id@{{.Provider.project}}.iam.gserviceaccount.com",
"description": "Service Account Description",
"name": "projects/{{.Provider.project}}/serviceAccounts/service-account-id@{{.Provider.project}}.iam.gserviceaccount.com",
"projectId": "{{.Provider.project}}",
"uniqueId": "108592438577779299646"
}
}
}
]
Loading