Skip to content

Commit

Permalink
Add project_from_id provider-defined function (GoogleCloudPlatform#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SarahFrench authored and BBBmau committed Feb 28, 2024
1 parent c8f1744 commit 12630a4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
6 changes: 3 additions & 3 deletions mmv1/third_party/terraform/functions/project_from_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func (f ProjectFromIdFunction) Metadata(ctx context.Context, req function.Metada

func (f ProjectFromIdFunction) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) {
resp.Definition = function.Definition{
Summary: "Returns the project within a provided resource id, self link, or OP style resource name.",
Description: "Takes a single string argument, which should be a resource id, self link, or OP style resource name. This function will either return the project name from the input string or raise an error due to no project being present in the string. The function uses the presence of \"projects/{{project}}/\" in the input string to identify the project name, e.g. when the function is passed the id \"projects/my-project/zones/us-central1-c/instances/my-instance\" as an argument it will return \"my-project\".",
Summary: "Returns the project within a provided resource's id, resource URI, self link, or full resource name.",
Description: "Takes a single string argument, which should be a resource's id, resource URI, self link, or full resource name. This function will either return the project name from the input string or raise an error due to no project being present in the string. The function uses the presence of \"projects/{{project}}/\" in the input string to identify the project name, e.g. when the function is passed the id \"projects/my-project/zones/us-central1-c/instances/my-instance\" as an argument it will return \"my-project\".",
Parameters: []function.Parameter{
function.StringParameter{
Name: "id",
Description: "A string of a resource's id, a resource's self link, or an OP style resource name. For example, \"projects/my-project/zones/us-central1-c/instances/my-instance\", \"https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/instances/my-instance\" and \"//gkehub.googleapis.com/projects/my-project/locations/us-central1/memberships/my-membership\" are valid values",
Description: "A string of a resource's id, resource URI, self link, or full resource name. For example, \"projects/my-project/zones/us-central1-c/instances/my-instance\", \"https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/instances/my-instance\" and \"//gkehub.googleapis.com/projects/my-project/locations/us-central1/memberships/my-membership\" are valid values",
},
},
Return: function.StringReturn{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestFunctionRun_project_from_id(t *testing.T) {
// Happy path inputs
validId := fmt.Sprintf("projects/%s/zones/us-central1-c/instances/my-instance", projectId)
validSelfLink := fmt.Sprintf("https://www.googleapis.com/compute/v1/%s", validId)
validOpStyleResourceName := fmt.Sprintf("//gkehub.googleapis.com/projects/%s/locations/us-central1/memberships/my-membership", projectId)

// Unhappy path inputs
repetitiveInput := fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/projects/not-this-1/projects/not-this-2/instances/my-instance", projectId) // Multiple /projects/{{project}}/
Expand All @@ -46,6 +47,14 @@ func TestFunctionRun_project_from_id(t *testing.T) {
Result: function.NewResultData(types.StringValue(projectId)),
},
},
"it returns the expected output value when given a valid OP style resource name input": {
request: function.RunRequest{
Arguments: function.NewArgumentsData([]attr.Value{types.StringValue(validOpStyleResourceName)}),
},
expected: function.RunResponse{
Result: function.NewResultData(types.StringValue(projectId)),
},
},
"it returns a warning and the first submatch when given repetitive input": {
request: function.RunRequest{
Arguments: function.NewArgumentsData([]attr.Value{types.StringValue(repetitiveInput)}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,43 @@ description: |-

# Function: project_from_id

Returns the project within a provided resource id, self link, or OP style resource name.
Returns the project within a provided resource's id, resource URI, self link, or full resource name.

For more information about using provider-defined functions with Terraform [see the official documentation](https://developer.hashicorp.com/terraform/plugin/framework/functions/concepts).

## Example Usage

### Use with the `google` provider

```terraform
terraform {
required_providers {
google = {
source = "hashicorp/google"
}
}
}
# Value is "my-project"
output "function_output" {
value = provider::google::project_from_id("https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/instances/my-instance")
}
```

### Use with the `google-beta` provider

```terraform
terraform {
required_providers {
google = {
source = "hashicorp/google"
}
}
required_providers {
google-beta = {
source = "hashicorp/google-beta"
}
}
}
# Value is "my-project"
output "function_output" {
value = provider::google::project_from_id("https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/instances/my-instance")
value = provider::google-beta::project_from_id("https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/instances/my-instance")
}
```

Expand All @@ -33,7 +54,7 @@ project_from_id(id string) string

## Arguments

1. `id` (String) A string of a resource's id, a resource's self link, or an OP style resource name. These are all valid values:
1. `id` (String) A string of a resource's id, resource URI, self link, or full resource name. For example, these are all valid values:

* `"projects/my-project/zones/us-central1-c/instances/my-instance"`
* `"https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/instances/my-instance"`
Expand Down

0 comments on commit 12630a4

Please sign in to comment.