in internal/provider/host_resource.go [286:388]
func (r *hostResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan
var plan vcenterHostRest
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
reqVcenterHost := backupdr.HostRest{
Hostname: plan.Hostname.ValueString(),
Hosttype: plan.Hosttype.ValueString(),
Friendlypath: plan.Friendlypath.ValueString(),
Ipaddress: plan.Ipaddress.ValueString(),
}
if plan.Hypervisoragent != nil {
reqVcenterHost.Hypervisoragent = &backupdr.AgentRest{
Username: plan.Hypervisoragent.Username.ValueString(),
Password: plan.Hypervisoragent.Password.ValueString(),
}
}
reqVcenterHost.Sources = append(reqVcenterHost.Sources, backupdr.HostRest{
Clusterid: plan.ApplianceClusterID.ValueString(),
})
// Generate API request body from plan
reqBody := backupdr.HostApiCreateHostOpts{
Body: optional.NewInterface(reqVcenterHost),
}
// Create new vCenter Host
respObject, _, err := r.client.HostApi.CreateHost(r.authCtx, &reqBody)
if err != nil {
resp.Diagnostics.AddError(
"Error creating vCenter Host",
"Could not create vCenter Host, unexpected error: "+err.Error(),
)
return
}
// Map response body to schema and populate Computed attribute values
plan.ID = types.StringValue(respObject.Id)
plan.Href = types.StringValue(respObject.Href)
// plan.Agents = types.BoolValue(respObject.Stale)
plan.Modifydate = types.Int64Value(respObject.Modifydate)
plan.Autoupgrade = types.StringValue(respObject.Autoupgrade)
plan.CertRevoked = types.BoolValue(respObject.CertRevoked)
plan.Clusterid = types.StringValue(respObject.Clusterid)
plan.Dbauthentication = types.BoolValue(respObject.Dbauthentication)
plan.Diskpref = types.StringValue(respObject.Diskpref)
plan.Friendlypath = types.StringValue(respObject.Friendlypath)
plan.Hasagent = types.BoolValue(respObject.Hasagent)
plan.Hostname = types.StringValue(respObject.Hostname)
plan.Hosttype = types.StringValue(respObject.Hosttype)
plan.Ipaddress = types.StringValue(respObject.Ipaddress)
plan.IsClusterNode = types.BoolValue(respObject.IsClusterNode)
plan.Isclusterhost = types.BoolValue(respObject.Isclusterhost)
plan.IsShadowHost = types.BoolValue(respObject.IsShadowHost)
plan.Isesxhost = types.BoolValue(respObject.Isesxhost)
plan.Isproxyhost = types.BoolValue(respObject.Isproxyhost)
plan.Isvcenterhost = types.BoolValue(respObject.Isvcenterhost)
plan.Isvm = types.BoolValue(respObject.Isvm)
plan.Maxjobs = types.Int64Value(int64(respObject.Maxjobs))
plan.Name = types.StringValue(respObject.Name)
plan.Originalhostid = types.StringValue(respObject.Originalhostid)
plan.PkiState = types.StringValue(respObject.PkiState)
plan.Sourcecluster = types.StringValue(respObject.Sourcecluster)
plan.Srcid = types.StringValue(respObject.Srcid)
plan.Svcname = types.StringValue(respObject.Svcname)
plan.Transport = types.StringValue(respObject.Transport)
plan.Uniquename = types.StringValue(respObject.Uniquename)
plan.Zone = types.StringValue(respObject.Zone)
plan.Hypervisoragent.Haspassword = types.BoolValue(respObject.Hypervisoragent.Haspassword)
plan.Hypervisoragent.Agenttype = types.StringValue(respObject.Hypervisoragent.Agenttype)
plan.Hypervisoragent.Hasalternatekey = types.BoolValue(respObject.Hypervisoragent.Hasalternatekey)
// plan.Agents = []types.Object{{
// Agenttype: types.StringValue(respObject.Agents[0].Agenttype),
// Hasalternatekey: types.BoolValue(respObject.Agents[0].Hasalternatekey),
// Haspassword: types.BoolValue(respObject.Agents[0].Haspassword),
// Username: types.StringValue(respObject.Agents[0].Username),
// Password: types.StringNull(),
// }}
// plan.Sources[0].ID = types.StringValue(respObject.Sources[0].Id)
// plan.Sources[0].Href = types.StringValue(respObject.Sources[0].Href)
// plan.Appliance = &ClusterRestRef{
// // Clusterid: types.StringValue(respObject.Appliance.Clusterid),
// ID: types.StringValue(respObject.Appliance.Id),
// Href: types.StringValue(respObject.Appliance.Href),
// }
// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}