From 8ce6d823e726cd467f4951121020fd1ceb3639eb Mon Sep 17 00:00:00 2001 From: Shawn Castrianni Date: Wed, 8 May 2024 13:34:19 -0500 Subject: [PATCH] Add workspace data source --- docs/data-sources/workspace.md | 15 +++++ postman/client/workspace.go | 4 ++ postman/data_source_workspace.go | 100 +++++++++++++++++++++++++++++++ postman/provider.go | 4 +- test/main.tf | 71 ++++++++++++---------- 5 files changed, 162 insertions(+), 32 deletions(-) create mode 100644 docs/data-sources/workspace.md create mode 100644 postman/data_source_workspace.go diff --git a/docs/data-sources/workspace.md b/docs/data-sources/workspace.md new file mode 100644 index 0000000..b81fd2d --- /dev/null +++ b/docs/data-sources/workspace.md @@ -0,0 +1,15 @@ +# Data Source: postman_workspace +Represents a workspace +## Example usage +```hcl +data "postman_workspace" "example" { + search_name = "Work" + type = "team" +} +``` +## Argument Reference +* `search_name` - **(Optional, String)** The search string to apply to the name of the workspace. Uses contains. +* `name` - **(Optional, String)** The filter string to apply to the name of the workspace. Uses equality. +* `type` - **(Optional, String)** The type of the workspace to filter on. Allowed values: `personal`, `private`, `public`, `team`, `partner`. +## Attribute Reference +* `id` - **(String)** Guid diff --git a/postman/client/workspace.go b/postman/client/workspace.go index 7e21427..f6f3c25 100644 --- a/postman/client/workspace.go +++ b/postman/client/workspace.go @@ -15,3 +15,7 @@ type Workspace struct { type WorkspaceContainer struct { Child Workspace `json:"workspace"` } + +type WorkspaceCollection struct { + Data []Workspace `json:"workspaces"` +} diff --git a/postman/data_source_workspace.go b/postman/data_source_workspace.go new file mode 100644 index 0000000..2821018 --- /dev/null +++ b/postman/data_source_workspace.go @@ -0,0 +1,100 @@ +package postman + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/scastria/terraform-provider-postman/postman/client" + "net/http" + "net/url" + "strings" +) + +func dataSourceWorkspace() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceWorkspaceRead, + Schema: map[string]*schema.Schema{ + "search_name": { + Type: schema.TypeString, + Optional: true, + }, + "name": { + Type: schema.TypeString, + Optional: true, + }, + "type": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{"personal", "private", "public", "team", "partner"}, false), + }, + }, + } +} + +func dataSourceWorkspaceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + var diags diag.Diagnostics + c := m.(*client.Client) + requestQuery := url.Values{} + filterType, ok := d.GetOk("type") + if ok { + requestQuery["type"] = []string{filterType.(string)} + } + requestPath := fmt.Sprintf(client.WorkspacePath) + body, err := c.HttpRequest(ctx, http.MethodGet, requestPath, requestQuery, nil, &bytes.Buffer{}) + if err != nil { + d.SetId("") + return diag.FromErr(err) + } + retVals := &client.WorkspaceCollection{} + err = json.NewDecoder(body).Decode(retVals) + if err != nil { + d.SetId("") + return diag.FromErr(err) + } + //Check for a quick exit + if len(retVals.Data) == 0 { + d.SetId("") + return diag.FromErr(fmt.Errorf("No workspace exists with that filter criteria")) + } + //Do manual searching + filteredList := []client.Workspace{} + searchName, ok := d.GetOk("search_name") + if ok { + searchNameLower := strings.ToLower(searchName.(string)) + for _, w := range retVals.Data { + if strings.Contains(strings.ToLower(w.Name), searchNameLower) { + filteredList = append(filteredList, w) + } + } + retVals.Data = filteredList + filteredList = []client.Workspace{} + } + name, ok := d.GetOk("name") + if ok { + nameLower := strings.ToLower(name.(string)) + for _, w := range retVals.Data { + if strings.ToLower(w.Name) == nameLower { + filteredList = append(filteredList, w) + } + } + retVals.Data = filteredList + filteredList = []client.Workspace{} + } + numWorkspaces := len(retVals.Data) + if numWorkspaces > 1 { + d.SetId("") + return diag.FromErr(fmt.Errorf("Filter criteria does not result in a single workspace")) + } else if numWorkspaces != 1 { + d.SetId("") + return diag.FromErr(fmt.Errorf("No workspace exists with that filter criteria")) + } + retVal := retVals.Data[0] + d.Set("name", retVal.Name) + d.Set("type", retVal.Type) + d.SetId(retVal.Id) + return diags +} diff --git a/postman/provider.go b/postman/provider.go index 006744b..5e1fa3c 100644 --- a/postman/provider.go +++ b/postman/provider.go @@ -33,7 +33,9 @@ func Provider() *schema.Provider { "postman_request": resourceRequest(), "postman_collection_sort": resourceCollectionSort(), }, - DataSourcesMap: map[string]*schema.Resource{}, + DataSourcesMap: map[string]*schema.Resource{ + "postman_workspace": dataSourceWorkspace(), + }, ConfigureContextFunc: providerConfigure, } } diff --git a/test/main.tf b/test/main.tf index e0294d4..a345f76 100644 --- a/test/main.tf +++ b/test/main.tf @@ -9,29 +9,38 @@ terraform { provider "postman" { } -resource "postman_workspace" "Workspace" { - name = "ShawnTest" - type = "personal" +data "postman_workspace" "Workspace" { +# name = "Data Team APIs" + search_name = "place" } -resource "postman_collection" "Collection" { - workspace_id = postman_workspace.Workspace.id - name = "ShawnTest" - description = "Desc" - var { - key = "url_base" - value = "https://postman-echo.com" - } - pre_request_script = [ - "script1", - "script2" - ] - post_response_script = [ - "script1", - "script2" - ] +output "workspace_id" { + value = data.postman_workspace.Workspace } +# resource "postman_workspace" "Workspace" { +# name = "ShawnTest" +# type = "personal" +# } + +# resource "postman_collection" "Collection" { +# workspace_id = postman_workspace.Workspace.id +# name = "ShawnTest" +# description = "Desc" +# var { +# key = "url_base" +# value = "https://postman-echo.com" +# } +# pre_request_script = [ +# "script1", +# "script2" +# ] +# post_response_script = [ +# "script1", +# "script2" +# ] +# } + # resource "postman_collection" "Collection2" { # workspace_id = postman_workspace.Workspace.id # name = "ShawnTest2" @@ -42,11 +51,11 @@ resource "postman_collection" "Collection" { # ] # } -resource "postman_folder" "Folder" { - for_each = toset(["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]) - collection_id = postman_collection.Collection.collection_id - name = each.key -} +# resource "postman_folder" "Folder" { +# for_each = toset(["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]) +# collection_id = postman_collection.Collection.collection_id +# name = each.key +# } # resource "postman_folder" "ScriptFolder" { # collection_id = postman_collection.Collection.collection_id # name = "WithScripts" @@ -56,13 +65,13 @@ resource "postman_folder" "Folder" { # ] # } -resource "postman_collection_sort" "CollectionSort" { - collection_id = postman_collection.Collection.collection_id - order = "ASC" - hash = timestamp() - case_sensitive = true - depends_on = [postman_folder.Folder] -} +# resource "postman_collection_sort" "CollectionSort" { +# collection_id = postman_collection.Collection.collection_id +# order = "ASC" +# hash = timestamp() +# case_sensitive = true +# depends_on = [postman_folder.Folder] +# } # resource "postman_request" "Request" { # collection_id = postman_collection.Collection.collection_id