Skip to content

Commit

Permalink
Add workspace data source
Browse files Browse the repository at this point in the history
  • Loading branch information
scastrianni committed May 8, 2024
1 parent bfb6a01 commit 8ce6d82
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 32 deletions.
15 changes: 15 additions & 0 deletions docs/data-sources/workspace.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions postman/client/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ type Workspace struct {
type WorkspaceContainer struct {
Child Workspace `json:"workspace"`
}

type WorkspaceCollection struct {
Data []Workspace `json:"workspaces"`
}
100 changes: 100 additions & 0 deletions postman/data_source_workspace.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 3 additions & 1 deletion postman/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
71 changes: 40 additions & 31 deletions test/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit 8ce6d82

Please sign in to comment.