@@ -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}}
19111910func (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}}
19311980func (r Resource ) CaiAssetNameTemplate (productBackendName string ) string {
0 commit comments