|
| 1 | +package metadata |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/GoogleCloudPlatform/magic-modules/mmv1/api" |
| 5 | +) |
| 6 | + |
| 7 | +// FromResource returns a Metadata object based on a Resource. |
| 8 | +func FromResource(r api.Resource) Metadata { |
| 9 | + m := Metadata{ |
| 10 | + Resource: r.TerraformName(), |
| 11 | + GenerationType: "mmv1", |
| 12 | + SourceFile: r.SourceYamlFile, |
| 13 | + ApiServiceName: r.ProductMetadata.ServiceName(), |
| 14 | + ApiVersion: r.ProductMetadata.ServiceVersion(), |
| 15 | + ApiResourceTypeKind: r.ApiResourceTypeKind, |
| 16 | + CaiAssetNameFormat: r.CAIFormatOverride(), |
| 17 | + ApiVariantPatterns: r.ApiVariantPatterns, |
| 18 | + AutogenStatus: r.AutogenStatus != "", |
| 19 | + Fields: FromProperties(r.LeafProperties()), |
| 20 | + } |
| 21 | + |
| 22 | + if m.ApiVersion == "" { |
| 23 | + m.ApiVersion = r.ServiceVersion() |
| 24 | + } |
| 25 | + if m.ApiResourceTypeKind == "" { |
| 26 | + m.ApiResourceTypeKind = r.Name |
| 27 | + } |
| 28 | + |
| 29 | + if r.HasSelfLink { |
| 30 | + m.Fields = append(m.Fields, Field{ |
| 31 | + ApiField: "selfLink", |
| 32 | + }) |
| 33 | + } |
| 34 | + return m |
| 35 | +} |
| 36 | + |
| 37 | +// Metadata represents a metadata.yaml file for a single Terraform resource. |
| 38 | +type Metadata struct { |
| 39 | + // The name of the Terraform resource. For example, "google_cloudfunctions2_function". |
| 40 | + Resource string `yaml:"resource"` |
| 41 | + |
| 42 | + // The generation method used to create the Terraform resource. For example, "mmv1", "dcl", "handwritten". |
| 43 | + GenerationType string `yaml:"generation_type"` |
| 44 | + |
| 45 | + // The source file of this metadata. This will only be set for generated resources, and will be the yaml file that contains the resource definition. |
| 46 | + SourceFile string `yaml:"source_file"` |
| 47 | + |
| 48 | + // The base name of the API used for this resource. For example, "cloudfunctions.googleapis.com". |
| 49 | + ApiServiceName string `yaml:"api_service_name"` |
| 50 | + |
| 51 | + // The version of the API used for this resource. For example, "v2". |
| 52 | + ApiVersion string `yaml:"api_version"` |
| 53 | + |
| 54 | + // The API "resource type kind" used for this resource. For example, "Function". |
| 55 | + ApiResourceTypeKind string `yaml:"api_resource_type_kind"` |
| 56 | + |
| 57 | + // The custom CAI asset name format for this resource is typically specified (for example, //cloudsql.googleapis.com/projects/{{project}}/instances/{{name}}). |
| 58 | + // This will only have a value if it's different than the Terraform resource ID format. |
| 59 | + CaiAssetNameFormat string `yaml:"cai_asset_name_format,omitempty"` |
| 60 | + |
| 61 | + // The API URL patterns used by this resource that represent variants. For example, "folders/{folder}/feeds/{feed}". Each pattern must match the value defined |
| 62 | + // in the API exactly. The use of `api_variant_patterns` is only meaningful when the resource type has multiple parent types available. |
| 63 | + ApiVariantPatterns []string `yaml:"api_variant_patterns,omitempty"` |
| 64 | + |
| 65 | + // Whether the resource was autogenerated from OpenAPI specs. |
| 66 | + AutogenStatus bool `yaml:"autogen_status,omitempty"` |
| 67 | + |
| 68 | + // List of fields on the resource. |
| 69 | + Fields []Field `yaml:"fields"` |
| 70 | +} |
0 commit comments