Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #15 from coveo/resource-ui-script
Browse files Browse the repository at this point in the history
Added UI Script resource
  • Loading branch information
mikegron authored Feb 21, 2019
2 parents 89cce4f + eefacf2 commit d1302b5
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
15 changes: 15 additions & 0 deletions servicenow/client/client_ui_script.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package client

// EndpointUIScript is the endpoint to manage UI Script records.
const EndpointUIScript = "sys_ui_script.do"

// UIScript is the json response for a UI Script in ServiceNow.
type UIScript struct {
BaseResult
Name string `json:"script_name"`
APIName string `json:"name,omitempty"`
Description string `json:"description"`
Script string `json:"script"`
Active bool `json:"active,string"`
UIType string `json:"ui_type"` // All: 10, Mobile: 1, Desktop 0
}
1 change: 1 addition & 0 deletions servicenow/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func Provider() *schema.Provider {
"servicenow_system_property_relation": resources.ResourceSystemPropertyRelation(),
"servicenow_ui_macro": resources.ResourceUIMacro(),
"servicenow_ui_page": resources.ResourceUIPage(),
"servicenow_ui_script": resources.ResourceUIScript(),
"servicenow_widget": resources.ResourceWidget(),
"servicenow_widget_dependency": resources.ResourceWidgetDependency(),
"servicenow_widget_dependency_relation": resources.ResourceWidgetDependencyRelation(),
Expand Down
144 changes: 144 additions & 0 deletions servicenow/resources/resource_ui_script.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package resources

import (
"github.com/coveo/terraform-provider-servicenow/servicenow/client"
"github.com/hashicorp/terraform/helper/schema"
)

const uiScriptName = "name"
const uiScriptDescription = "description"
const uiScriptScript = "script"
const uiScriptActive = "active"
const uiScriptUIType = "type"
const uiScriptAPIName = "api_name"

// ResourceUIScript manages a UI Script in ServiceNow which can be added to any other UI component.
func ResourceUIScript() *schema.Resource {
return &schema.Resource{
Create: createResourceUIScript,
Read: readResourceUIScript,
Update: updateResourceUIScript,
Delete: deleteResourceUIScript,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
uiScriptName: {
Type: schema.TypeString,
Required: true,
},
uiScriptDescription: {
Type: schema.TypeString,
Optional: true,
Default: "",
},
uiScriptScript: {
Type: schema.TypeString,
Required: true,
},
uiScriptActive: {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
uiScriptUIType: {
Type: schema.TypeString,
Optional: true,
Default: "all",
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
warns, errs = validateStringValue(val.(string), key, []string{"all", "desktop", "mobile"})
return
},
},
uiScriptAPIName: {
Type: schema.TypeString,
Computed: true,
},
commonScope: getScopeSchema(),
},
}
}

func readResourceUIScript(data *schema.ResourceData, serviceNowClient interface{}) error {
snowClient := serviceNowClient.(*client.ServiceNowClient)
uiScript := &client.UIScript{}
if err := snowClient.GetObject(client.EndpointUIScript, data.Id(), uiScript); err != nil {
data.SetId("")
return err
}

resourceFromUIScript(data, uiScript)

return nil
}

func createResourceUIScript(data *schema.ResourceData, serviceNowClient interface{}) error {
snowClient := serviceNowClient.(*client.ServiceNowClient)
uiScript := resourceToUIScript(data)
if err := snowClient.CreateObject(client.EndpointUIScript, uiScript); err != nil {
return err
}

resourceFromUIScript(data, uiScript)

return readResourceUIScript(data, serviceNowClient)
}

func updateResourceUIScript(data *schema.ResourceData, serviceNowClient interface{}) error {
snowClient := serviceNowClient.(*client.ServiceNowClient)
if err := snowClient.UpdateObject(client.EndpointUIScript, resourceToUIScript(data)); err != nil {
return err
}

return readResourceUIScript(data, serviceNowClient)
}

func deleteResourceUIScript(data *schema.ResourceData, serviceNowClient interface{}) error {
snowClient := serviceNowClient.(*client.ServiceNowClient)
return snowClient.DeleteObject(client.EndpointUIScript, data.Id())
}

func resourceFromUIScript(data *schema.ResourceData, script *client.UIScript) {
var typeString string
switch script.UIType {
case "1":
typeString = "mobile"
case "0":
typeString = "desktop"
default:
typeString = "all"
}

data.SetId(script.ID)
data.Set(uiScriptName, script.Name)
data.Set(uiScriptDescription, script.Description)
data.Set(uiScriptScript, script.Script)
data.Set(uiScriptActive, script.Active)
data.Set(uiScriptUIType, typeString)
data.Set(uiScriptAPIName, script.APIName)
}

func resourceToUIScript(data *schema.ResourceData) *client.UIScript {
var typeInt string
switch data.Get(uiScriptUIType).(string) {
case "mobile":
typeInt = "1"
case "desktop":
typeInt = "0"
default:
typeInt = "10"
}

uiScript := client.UIScript{
Name: data.Get(uiScriptName).(string),
Description: data.Get(uiScriptDescription).(string),
Script: data.Get(uiScriptScript).(string),
Active: data.Get(uiScriptActive).(bool),
UIType: typeInt,
}
uiScript.ID = data.Id()
uiScript.Scope = data.Get(commonScope).(string)
return &uiScript
}

0 comments on commit d1302b5

Please sign in to comment.