Crud resourceHouseCreate()
Function signature
Line 1: the argument
d schema.ResourceData
object is used to read and set the resource's attributes and manage its state in Terraform.Line 1:
diag.Diagnostics
is an object storing diagnostic data from the go packagediag
."Package
diag
implements diagnostic functionality, which is a practitioner feedback mechanism for providers. It is designed for display in Terraform user interfaces, rather than logging based feedback, which is generally saved to a file for later inspection and troubleshooting." (Terraform official docs)
payload
, lines 6-12: when aresource
is declared somewhere in a terraform file (main.tf
in our bootcamp), the Terraform SDK for custom plugin grabs the attribute data from ourresource
declaration and passes that into theresourceHouseCreate()
function as an argument. Inside this Create function, the variablepayload
is constructed using theschema.ResourceData
's methodGet()
function.A Method is a function that belongs to a specific object type in Golang.
payloadBytes, err
, lines 13-16: then the payload will be parsed into json format. Any errors captured will be stored in the variablediag
, which will be returned when the function execution completes.Lines 21-51: interacting with TerraTowns API through HTTP
Lines 22-24: Construct a HTTP Request
A HTTP POST request is created using the
http.NewRequest
function (Remember?http
is a package we imported in the beginning).The protocol method is POST because we have to push the data of a new house that is created to the TerraTowns' database.
The function
bytes.NewBuffer
creates a newbytes.Buffer
. Buffer is a bit complicated and deep concept. Just understand that a buffer is a type of data structure that is best for sending data through HTTP request. So we are kind of "parsing" thepayload
data into a buffer so the HTTP POST request can send it over to the destination.
Lines 27-29: Set headers
Headers are some extra information we optionally send along the body of data to the destination API. When we send a letter to someone, we include our address and our name so the recipient knows who they get the letter from.
Content-Type
: informs the destination server of the data structure, helping the server to parse the data correctly.Accept
: also informs the destination server of the data structure the sender (our terraform) expects to get in the response.
Lines 32-37:
Line 32: create an
http
client instance.Line 33:
client.Do(req)
is a method call on the client instance and sends the HTTP POST request to the specified URL. The URL endpoint is already included in thereq
variable as previously assigned in line 21.Line 34: if there is an error (if error is not
nil
), the error information will be included indiag
.Line 37:
defer
schedules a function call to be executed just before the surrounding function returns. This means that thisdefer
statement will only run AFTER the line 52d.SetId(homeUUID)
is executed - right before theresourceHouseCreate
function returns.
Lines 40-42: Parse response JSON
Line 40: create a variable
responseData
which is amap[]
with keys of type string and values of any data type (interface{}
).Lines 41-42:
json.NewDecoder
decodes the response body. If there is any error, it will be stored indiag
.
Lines 46-47: Check if the HTTP status is OK.
Line 46: if the HTTP status is not OK,
Line 47: the error information (
resp.StatusCode
,resp.Status
,responseData
) will be stored indiag
.
Lines 51-52: Handle response status
Line 51: After having dealt with the errors, store the
uuid
(responseData["uuid"]
) and assert the type (.(string)
)Line 52: Then set the
homeUUID
value to the ID ofd
.
Line 54: this
resourceHouseCreate()
function interacts with the cloud service's database so it doesn't really return anything about our house. Instead, it returns thediag
which contains the error information. Remember, line 37 will run right before line 54, as the execution has been put on hold with thedefer
statement.
Resources
Go
Package
diag
Last updated