Skip to content

Commit

Permalink
Add support for JS scripts on collections
Browse files Browse the repository at this point in the history
  • Loading branch information
scastrianni committed May 6, 2024
1 parent e95803d commit 07383a4
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 6 deletions.
6 changes: 6 additions & 0 deletions docs/resources/collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ resource "postman_collection" "example" {
key = "url_base"
value = "https://postman-echo.com"
}
pre_request_script = [
"script line 1",
"script line 2"
]
}
```
## Argument Reference
* `workspace_id` - **(Required, ForceNew, String)** The id of the parent workspace.
* `name` - **(Required, String)** The name of the collection.
* `description` - **(Optional, String)** The description of the collection.
* `pre_request_script` - **(Optional, List of String)** The JS script to run before the request.
* `post_response_script` - **(Optional, List of String)** The JS script to run after the response (previously called Test scripts).
* `var` - **(Optional, list{var})** Configuration block for a variable. Can be specified multiple times for each var. Each block supports the fields documented below.
## var
* `key` - **(Required, String)** The name of the variable.
Expand Down
1 change: 1 addition & 0 deletions postman/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
const (
APIKeyHeader = "X-API-Key"
ApplicationJson = "application/json"
TextJS = "text/javascript"
Bearer = "Bearer"
PostmanDomain = "api.getpostman.com"
IdSeparator = ":"
Expand Down
21 changes: 17 additions & 4 deletions postman/client/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,31 @@ const (
type Collection struct {
Info CollectionInfo `json:"info"`
Variables []Variable `json:"variable"`
Events []Event `json:"event"`
}

type CollectionUpdate struct {
Info CollectionInfo `json:"info"`
Variables []Variable `json:"variables"`
Events []Event `json:"events"`
}

type CollectionCreate struct {
Info CollectionInfo `json:"info"`
// Not actually strings, but we don't need to care about the structure
Items []string `json:"item"`
Variables []Variable `json:"variables"`
Info CollectionInfo `json:"info"`
Items []interface{} `json:"item"`
Variables []Variable `json:"variables"`
Events []Event `json:"events"`
}

type Event struct {
Listen string `json:"listen"`
Script Script `json:"script"`
}

type Script struct {
Id string `json:"id"`
Type string `json:"type"`
Exec []string `json:"exec"`
}

type CollectionInfo struct {
Expand Down
76 changes: 74 additions & 2 deletions postman/resource_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ func resourceCollection() *schema.Resource {
},
},
},
"pre_request_script": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"post_response_script": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
Expand All @@ -89,13 +103,36 @@ func fillCollectionUpdate(c *client.CollectionUpdateContainer, d *schema.Resourc
})
}
}
c.Child.Events = []client.Event{}
preRequestScript, ok := d.GetOk("pre_request_script")
if ok {
c.Child.Events = append(c.Child.Events, client.Event{
Listen: "prerequest",
Script: client.Script{
Id: "prerequest",
Type: client.TextJS,
Exec: convertInterfaceArrayToStringArray(preRequestScript.([]interface{})),
},
})
}
postResponseScript, ok := d.GetOk("post_response_script")
if ok {
c.Child.Events = append(c.Child.Events, client.Event{
Listen: "test",
Script: client.Script{
Id: "test",
Type: client.TextJS,
Exec: convertInterfaceArrayToStringArray(postResponseScript.([]interface{})),
},
})
}
}

func fillCollectionCreate(c *client.CollectionCreateContainer, d *schema.ResourceData) {
c.Child.Info.WorkspaceId = d.Get("workspace_id").(string)
c.Child.Info.Name = d.Get("name").(string)
c.Child.Info.Schema = client.CollectionSchema
c.Child.Items = []string{}
c.Child.Items = []interface{}{}
description, ok := d.GetOk("description")
if ok {
c.Child.Info.Description = description.(string)
Expand All @@ -113,6 +150,29 @@ func fillCollectionCreate(c *client.CollectionCreateContainer, d *schema.Resourc
})
}
}
c.Child.Events = []client.Event{}
preRequestScript, ok := d.GetOk("pre_request_script")
if ok {
c.Child.Events = append(c.Child.Events, client.Event{
Listen: "prerequest",
Script: client.Script{
Id: "prerequest",
Type: client.TextJS,
Exec: convertInterfaceArrayToStringArray(preRequestScript.([]interface{})),
},
})
}
postResponseScript, ok := d.GetOk("post_response_script")
if ok {
c.Child.Events = append(c.Child.Events, client.Event{
Listen: "test",
Script: client.Script{
Id: "test",
Type: client.TextJS,
Exec: convertInterfaceArrayToStringArray(postResponseScript.([]interface{})),
},
})
}
}

func fillResourceDataFromCollection(c *client.CollectionContainer, d *schema.ResourceData) {
Expand All @@ -128,12 +188,24 @@ func fillResourceDataFromCollection(c *client.CollectionContainer, d *schema.Res
variableMap := map[string]interface{}{}
variableMap["key"] = variable.Key
variableMap["value"] = variable.Value
variableMap["type"] = variable.Type
variableMap["disabled"] = variable.Disabled
variables = append(variables, variableMap)
}
}
d.Set("var", variables)
preRequestScripts := []string{}
postResponseScripts := []string{}
if c.Child.Events != nil {
for _, event := range c.Child.Events {
if event.Listen == "prerequest" {
preRequestScripts = event.Script.Exec
} else if event.Listen == "test" {
postResponseScripts = event.Script.Exec
}
}
}
d.Set("pre_request_script", preRequestScripts)
d.Set("post_response_script", postResponseScripts)
}

func resourceCollectionCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down
8 changes: 8 additions & 0 deletions postman/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import (
"reflect"
)

func convertInterfaceArrayToStringArray(arr []interface{}) []string {
retVal := []string{}
for _, a := range arr {
retVal = append(retVal, a.(string))
}
return retVal
}

func convertSetToArray(set *schema.Set) []string {
setList := set.List()
retVal := []string{}
Expand Down
18 changes: 18 additions & 0 deletions test/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,26 @@ resource "postman_collection" "Collection" {
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"
# description = "Desc2"
# pre_request_script = [
# "script1",
# "script2"
# ]
# }

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
Expand Down

0 comments on commit 07383a4

Please sign in to comment.