Hi Matthew and Tristan,
I’m glad you are having some success with saving the script module! It would be fantastic if you could submit a Pull Request at https://github.com/OctopusDeploy/Library when your step template is ready to go.
With regards to accessing the body of the script module using the API, it looks like you are accessing the collection of script modules from api/libraryvariablesets
(or api/libraryvariablesets?contentType=ScriptModule
). This will return a result set like:
{
"ItemType": "LibraryVariableSet",
"IsStale": false,
"TotalResults": 1,
"ItemsPerPage": 30,
"Items": [
{
"Id": "LibraryVariableSets-1",
"Name": "My Script Module",
"Description": "Some description",
"VariableSetId": "variableset-LibraryVariableSets-1",
"ContentType": "ScriptModule",
"Templates": [],
"Links": {
"Self": "/api/libraryvariablesets/LibraryVariableSets-1",
"Variables": "/api/variables/variableset-LibraryVariableSets-1"
}
}
],
"Links": {
"Self": "/api/libraryvariablesets",
"Template": "/api/libraryvariablesets{?skip,contentType,take,ids}",
"Page.Current": "/api/libraryvariablesets?skip=0&take=30",
"Page.0": "/api/libraryvariablesets?skip=0&take=30"
}
}
As you mentioned, this contains fields like Id, Name and Description, but does not contain the body of the script module. To access the body of the script module, you will need to request the variables from the /api/variables
endpoint. In the above JSON result, this means following the link contained in Item.Links.Variables
. The script module is stored as a Variable Set that contains a single variable. The single variable represents the body of the script module.
So in this case, submitting a request to api/variables/variableset-LibraryVariableSets-1
will return a result like:
{
"Id": "variableset-LibraryVariableSets-1",
"OwnerId": "LibraryVariableSets-1",
"Version": 3,
"Variables": [
{
"Id": "03473d10-8a70-461c-81a7-fa4c7589edc2",
"Name": "Octopus.Script.Module[My Script Module]",
"Value": "function Say-Hello()\r\n{\r\n Write-Output \"Hello, Octopus!\"\r\n}\r\n",
"Scope": {},
"IsEditable": true,
"Prompt": null,
"Type": "String",
"IsSensitive": false
}
],
"ScopeValues": {...},
"Links": {
"Self": "/api/variables/variableset-LibraryVariableSets-1"
}
}
Here you can see the body of the script module can be accessed through Variables[0].Value
.
Feel free to ask if you have any other questions.
Tom