|
6 | 6 | "fmt"
|
7 | 7 | "regexp"
|
8 | 8 |
|
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
9 | 10 | transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
|
10 | 11 | )
|
11 | 12 |
|
@@ -236,6 +237,61 @@ func ParseZonalFieldValue(resourceType, fieldValue, projectSchemaField, zoneSche
|
236 | 237 | }, nil
|
237 | 238 | }
|
238 | 239 |
|
| 240 | +// Parses a zonal field supporting 5 different formats: |
| 241 | +// - https://www.googleapis.com/compute/ANY_VERSION/projects/{my_project}/zones/{zone}/{resource_type}/{resource_name} |
| 242 | +// - projects/{my_project}/zones/{zone}/{resource_type}/{resource_name} |
| 243 | +// - zones/{zone}/{resource_type}/{resource_name} |
| 244 | +// - resource_name |
| 245 | +// - "" (empty string). RelativeLink() returns empty if isEmptyValid is true. |
| 246 | +// |
| 247 | +// If the project is not specified, it first tries to get the project from the `projectSchemaField` and then fallback on the default project. |
| 248 | +// If the zone is not specified, it takes the value of `zoneSchemaField`. |
| 249 | +func ParseZonalFieldValueDiff(resourceType, fieldValue, projectSchemaField, zoneSchemaField string, d *schema.ResourceDiff, config *transport_tpg.Config, isEmptyValid bool) (*ZonalFieldValue, error) { |
| 250 | + r := regexp.MustCompile(fmt.Sprintf(ZonalLinkBasePattern, resourceType)) |
| 251 | + if parts := r.FindStringSubmatch(fieldValue); parts != nil { |
| 252 | + return &ZonalFieldValue{ |
| 253 | + Project: parts[1], |
| 254 | + Zone: parts[2], |
| 255 | + Name: parts[3], |
| 256 | + ResourceType: resourceType, |
| 257 | + }, nil |
| 258 | + } |
| 259 | + |
| 260 | + project, err := GetProjectFromDiff(d, config) |
| 261 | + if err != nil { |
| 262 | + return nil, err |
| 263 | + } |
| 264 | + |
| 265 | + r = regexp.MustCompile(fmt.Sprintf(ZonalPartialLinkBasePattern, resourceType)) |
| 266 | + if parts := r.FindStringSubmatch(fieldValue); parts != nil { |
| 267 | + return &ZonalFieldValue{ |
| 268 | + Project: project, |
| 269 | + Zone: parts[1], |
| 270 | + Name: parts[2], |
| 271 | + ResourceType: resourceType, |
| 272 | + }, nil |
| 273 | + } |
| 274 | + |
| 275 | + if len(zoneSchemaField) == 0 { |
| 276 | + return nil, fmt.Errorf("Invalid field format. Got '%s', expected format '%s'", fieldValue, fmt.Sprintf(GlobalLinkTemplate, "{project}", resourceType, "{name}")) |
| 277 | + } |
| 278 | + |
| 279 | + zone, ok := d.GetOk(zoneSchemaField) |
| 280 | + if !ok { |
| 281 | + zone = config.Zone |
| 282 | + if zone == "" { |
| 283 | + return nil, fmt.Errorf("A zone must be specified") |
| 284 | + } |
| 285 | + } |
| 286 | + |
| 287 | + return &ZonalFieldValue{ |
| 288 | + Project: project, |
| 289 | + Zone: zone.(string), |
| 290 | + Name: GetResourceNameFromSelfLink(fieldValue), |
| 291 | + ResourceType: resourceType, |
| 292 | + }, nil |
| 293 | +} |
| 294 | + |
239 | 295 | func GetProjectFromSchema(projectSchemaField string, d TerraformResourceData, config *transport_tpg.Config) (string, error) {
|
240 | 296 | res, ok := d.GetOk(projectSchemaField)
|
241 | 297 | if ok && projectSchemaField != "" {
|
|
0 commit comments