2 3 0 Custom Terraform Provider: Resource Skeleton (Go)
In this episode, Andrew defines validateUUID()
and providerConfigure()
functions, and sets up the skeleton of resource()
.
Code Notes
schema.Resource:
Resource()
represents a thing (entity) in Terraform that has a set of configurable attributes and a lifecycle (create, read, update and delete).
validateUUID()
validateUUID()
Line 1, function signature
Parameters
The parameter
v
is of typeinterface{}
, which can accept values of any data type.The parameter
k
should be a string.
Return value
This function will return two slices - a slice of strings (
ws
) and another of errors (errors
).
Line 2 value
The string from the
v
(interface{}
)'s argument will be assigned to the variablevalue
.:=
is Go's way of declaring a variable..(string)
is Go's way of casting a type to a value.a bit deeper: technically speaking, this is called "type assertion" in go and it is a bit different from casting in other languages.
Lines 3-4
if _, err
First, assign the two return values from the function
uuid.Parse(value)
to the variables_
anderr
. Note that the function uuid is coming from Google'suuid
package for Go (see the import statement in line 7 in the source code at the top).If the variable
err
's data type is notnil
(err != nil
), it means that there is an error. In that case, append the error values to theerrors
slice which will be later returned.
Line 6
return
statementNote that what will be returned is not declared after the
return
keyword. This is characteristic of Go, where it is not necessary to specify one as the return values are already declared in the function signature (which is thefunc
statement, line 1).
configureProvider()
configureProvider()
Function signature, line 1
Parameters
p
is an object ofschema.Provider
type, which is a Terraform provider.
Return values
Interestingly, this function returns another function, which is a
schema.ConfigureContextFunc
.Parameters: this function to be returned takes a context object from the
context
package, and aResourceData
object from theschema
package.context
: is a package used to manage and control the lifecycle of processes and requests in Go applications.
Lines 3-7: The function internally constructs an object called
config
which has three string values. This will be later used for authenticating our http requests and connecting to the cloud service (TerraTowns).Return values: this function returns a pointer to the
config
struct and anil
.nil
indicates that the configuration was successful.
Resources
Development workflow documentation: 2.3.0 Resource Skeleton
Last updated