Skip to content

Commit d8b6962

Browse files
authored
tgc-revival: decide the cai identifier automatically (#15085)
1 parent 958af83 commit d8b6962

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

mmv1/api/resource.go

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,8 @@ type TGCResource struct {
387387
TGCIgnoreTerraformEncoder bool `yaml:"tgc_ignore_terraform_encoder,omitempty"`
388388

389389
// [Optional] The parameter that uniquely identifies the resource.
390-
// Generally, it's safe to leave empty, in which case it defaults to `name`.
391-
// Other values are normally useful in cases where an object has a parent
392-
// and is identified by some non-name value, such as an ip+port pair.
390+
// Generally, it shouldn't be set when the identity can be decided.
391+
// Otherswise, it should be set.
393392
CaiIdentity string `yaml:"cai_identity,omitempty"`
394393
}
395394

@@ -1910,22 +1909,72 @@ func (r Resource) DefineAssetTypeForResourceInProduct() bool {
19101909
// For example: //monitoring.googleapis.com/v3/projects/{{project}}/services/{{service_id}}
19111910
func (r Resource) rawCaiAssetNameTemplate(productBackendName string) string {
19121911
caiBaseUrl := ""
1913-
caiId := "name"
1912+
caiId := ""
19141913
if r.CaiIdentity != "" {
19151914
caiId = r.CaiIdentity
1915+
} else {
1916+
caiId = r.getCaiId()
19161917
}
1918+
caiIdTemplate := fmt.Sprintf("{{%s}}", caiId)
19171919
if r.CaiBaseUrl != "" {
1918-
caiBaseUrl = fmt.Sprintf("%s/{{%s}}", r.CaiBaseUrl, caiId)
1920+
if caiId == "" || strings.Contains(r.CaiBaseUrl, caiIdTemplate) {
1921+
caiBaseUrl = r.CaiBaseUrl
1922+
} else {
1923+
caiBaseUrl = fmt.Sprintf("%s/%s", r.CaiBaseUrl, caiIdTemplate)
1924+
}
19191925
}
19201926
if caiBaseUrl == "" {
19211927
caiBaseUrl = r.SelfLink
19221928
}
19231929
if caiBaseUrl == "" {
1924-
caiBaseUrl = fmt.Sprintf("%s/{{%s}}", r.BaseUrl, caiId)
1930+
if caiId == "" || strings.Contains(r.BaseUrl, caiIdTemplate) {
1931+
caiBaseUrl = r.BaseUrl
1932+
} else {
1933+
caiBaseUrl = fmt.Sprintf("%s/%s", r.BaseUrl, caiIdTemplate)
1934+
}
19251935
}
19261936
return fmt.Sprintf("//%s.googleapis.com/%s", productBackendName, caiBaseUrl)
19271937
}
19281938

1939+
// Guesses the identifier of the resource, as "name" is not always the identifier
1940+
// For example, the cai identifier is feed_id in google_cloud_asset_folder_feed
1941+
func (r Resource) getCaiId() string {
1942+
for _, p := range r.AllUserProperties() {
1943+
if p.Name == "name" && !p.Output {
1944+
return "name"
1945+
}
1946+
}
1947+
1948+
// Get the last identifier extracted from selfLink
1949+
id := r.getCandidateCaiId(r.SelfLink)
1950+
if id != "" {
1951+
return id
1952+
}
1953+
1954+
// Get the last identifier extracted from createUrl
1955+
id = r.getCandidateCaiId(r.CreateUrl)
1956+
if id != "" {
1957+
return id
1958+
}
1959+
1960+
return ""
1961+
}
1962+
1963+
// Extracts the last identifier from the url, if it is not computed,
1964+
// then it is the candidate identifier
1965+
func (r Resource) getCandidateCaiId(url string) string {
1966+
identifiers := r.ExtractIdentifiers(url)
1967+
if len(identifiers) > 0 {
1968+
id := identifiers[len(identifiers)-1]
1969+
for _, p := range r.AllUserProperties() {
1970+
if google.Underscore(p.Name) == id && !p.Output {
1971+
return id
1972+
}
1973+
}
1974+
}
1975+
return ""
1976+
}
1977+
19291978
// Gets the Cai asset name template, which doesn't include version
19301979
// For example: //monitoring.googleapis.com/projects/{{project}}/services/{{service_id}}
19311980
func (r Resource) CaiAssetNameTemplate(productBackendName string) string {

mmv1/products/cloudasset/FolderFeed.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ custom_code:
4242
supports_indirect_user_project_override: true
4343
include_in_tgc_next_DO_NOT_USE: true
4444
cai_base_url: 'folders/{{folder}}/feeds'
45-
cai_identity: 'feed_id'
4645
tgc_ignore_terraform_encoder: true
4746
examples:
4847
- name: 'cloud_asset_folder_feed'

mmv1/products/cloudasset/OrganizationFeed.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ custom_code:
4242
supports_indirect_user_project_override: true
4343
include_in_tgc_next_DO_NOT_USE: true
4444
cai_base_url: 'organizations/{{org_id}}/feeds'
45-
cai_identity: 'feed_id'
4645
tgc_ignore_terraform_encoder: true
4746
examples:
4847
- name: 'cloud_asset_organization_feed'

mmv1/products/cloudasset/ProjectFeed.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ custom_code:
4040
custom_import: 'templates/terraform/custom_import/cloud_asset_feed.go.tmpl'
4141
include_in_tgc_next_DO_NOT_USE: true
4242
cai_base_url: 'projects/{{project}}/feeds'
43-
cai_identity: 'feed_id'
4443
tgc_ignore_terraform_encoder: true
4544
examples:
4645
- name: 'cloud_asset_project_feed'

0 commit comments

Comments
 (0)