Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
update read method for hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
badarsebard committed Mar 7, 2022
1 parent 7d007a5 commit 822d548
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
2 changes: 2 additions & 0 deletions xsoar/resource_ha_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func (r resourceHAGroup) Create(ctx context.Context, req tfsdk.CreateResourceReq
return
}

// todo: trigger the host installer build

var accountIds []attr.Value
for _, a := range haGroup.GetAccountIds() {
accountIds = append(accountIds, types.String{Value: a})
Expand Down
30 changes: 14 additions & 16 deletions xsoar/resource_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,6 @@ func (r resourceHost) Create(ctx context.Context, req tfsdk.CreateResourceReques
return
}

// Map response body to resource schema attribute
var result Host
result = Host{
Name: types.String{Value: hostName},
Expand All @@ -399,7 +398,6 @@ func (r resourceHost) Create(ctx context.Context, req tfsdk.CreateResourceReques
}

if host["host"].(string) != haGroupName.GetName() {
isHA = true
result.HAGroupName.Value = haGroupName.GetName()
} else {
result.HAGroupName.Null = true
Expand Down Expand Up @@ -462,8 +460,14 @@ func (r resourceHost) Read(ctx context.Context, req tfsdk.ReadResourceRequest, r
var hostId = host["id"].(string)
var hostGroupId = host["hostGroupId"].(string)

haGroup, _, err := r.p.client.DefaultApi.GetHAGroup(ctx, hostGroupId).Execute()
haGroupName, httpResponse, err := r.p.client.DefaultApi.GetHAGroup(ctx, hostGroupId).Execute()
if err != nil {
body, bodyErr := io.ReadAll(httpResponse.Body)
if bodyErr != nil {
log.Println("error reading body: " + bodyErr.Error())
return
}
log.Printf("code: %d status: %s headers: %s body: %s\n", httpResponse.StatusCode, httpResponse.Status, httpResponse.Header, string(body))
resp.Diagnostics.AddError(
"Error getting HA group",
"Could not get HA group: "+err.Error(),
Expand All @@ -477,30 +481,24 @@ func (r resourceHost) Read(ctx context.Context, req tfsdk.ReadResourceRequest, r
Id: types.String{Value: hostId},
InstallationTimeout: state.InstallationTimeout,
ExtraFlags: state.ExtraFlags,
NFSMount: state.NFSMount,
ServerUrl: state.ServerUrl,
SSHUser: state.SSHUser,
SSHKey: state.SSHKey,
}

var isHA = false
if host["host"].(string) != haGroup.GetName() {
isHA = true
result.HAGroupName.Value = haGroup.GetName()
if host["host"].(string) != haGroupName.GetName() {
result.HAGroupName.Value = haGroupName.GetName()
} else {
result.HAGroupName.Null = true
}

if len(host["elasticsearchAddress"].(string)) > 0 {
if isHA {
result.ElasticsearchUrl.Null = true
} else {
result.ElasticsearchUrl.Value = host["elasticsearchAddress"].(string)
}
result.ElasticsearchUrl.Value = host["elasticsearchAddress"].(string)
} else {
result.ElasticsearchUrl.Null = true
}

result.ServerUrl = state.ServerUrl
result.SSHUser = state.SSHUser
result.SSHKey = state.SSHKey

// Generate resource state struct
diags = resp.State.Set(ctx, result)
resp.Diagnostics.Append(diags...)
Expand Down

0 comments on commit 822d548

Please sign in to comment.