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

Commit

Permalink
finish out the data sources for account, host, ha group, and integrat…
Browse files Browse the repository at this point in the history
…ion instance; including acceptance tests.
  • Loading branch information
badarsebard committed Oct 24, 2021
1 parent e13bd9c commit 18a99b4
Show file tree
Hide file tree
Showing 15 changed files with 381 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .run/Test ACC.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<makefile filename="$PROJECT_DIR$/Makefile" target="testacc" workingDirectory="" arguments="">
<envs>
<env name="DEMISTO_BASE_URL" value="https://main.xsoar.local" />
<env name="DEMISTO_API_KEY" value="6ADCA625D00D6FC10F05B8BCFD4BC34C" />
<env name="DEMISTO_API_KEY" value="93C67EB97F30E6464DF1E5737F0470E0" />
<env name="DEMISTO_INSECURE" value="1" />
<env name="DEMISTO_HOST_KEYFILE" value="$PROJECT_DIR$/.vagrant/machines/host1/virtualbox/private_key" />
<env name="DEMISTO_HOST" value="host1.xsoar.local" />
Expand Down
30 changes: 29 additions & 1 deletion .run/provision_main.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
sudo /vagrant/local/installer.sh -- -multi-tenant -y -elasticsearch-url=http://elastic.xsoar.local:9200
sudo /vagrant/local/installer.sh -- -multi-tenant -y -elasticsearch-url=http://elastic.xsoar.local:9200 -tools=false
sudo cp /vagrant/local/license /tmp/demisto.lic
sudo chown demisto:demisto /tmp/demisto.lic
sudo cp /tmp/demisto.lic /var/lib/demisto/
sudo systemctl restart demisto

# set cookies
until curl --fail --cookie-jar cookie.txt 'https://main.xsoar.local/health' --insecure
do
sleep 5
done

# set admin password
XSRF=$(cat cookie.txt | grep XSRF | cut -f 7)
curl --cookie cookie.txt --cookie-jar cookie.txt 'https://main.xsoar.local/login' \
-H "X-XSRF-TOKEN: ${XSRF}" \
-H "Content-Type: application/json" \
--data-raw '{"user":"admin","password":"admin","newPassword":"Xsoar123","passwordValidator":"Xsoar123","loginFailed":true,"passwordExpired":true,"weakNewPassword":false,"reusedOldPassword":false,"userLockedOut":false,"userDisabled":false,"selfUnlockRemainingMinutes":0,"loading":false,"duoFactor":"","passcode":"","shouldShowRegularLogin":false,"userTimeZone":"America/New_York"}' \
--insecure

# create api key
curl -u "admin:Xsoar123" --cookie cookie.txt --cookie-jar cookie.txt 'https://main.xsoar.local/apikeys' \
-H "X-XSRF-TOKEN: ${XSRF}" \
-H "Content-Type: application/json" \
--data-raw '{"name":"tf","apikey":"93C67EB97F30E6464DF1E5737F0470E0"}' \
--insecure

# set dark mode
curl --cookie cookie.txt 'https://main.xsoar.local/user/update/preferences' \
-H 'Authorization: 93C67EB97F30E6464DF1E5737F0470E0' \
-H "Content-Type: application/json" \
--data-raw '{"id":"admin","homepage":"","investigationPage":"","disableHyperSearch":false,"editorStyle":"","timeZone":"","hours24":"","dateFormat":"","theme":"dark","helpSnippetDisabled":false,"shortcutsDisabled":false,"notificationsSettings":{"email":{"all":true},"pushNotifications":{"all":true}}}' \
--insecure
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ release:
install: build
mkdir -p ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH}
mv ${BINARY} ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH}
rm -rf examples/.terraform/ examples/.terraform.lock.hcl examples/*.tfstate
rm -rf examples/.terraform/ examples/.terraform.lock.hcl examples/*.tfstate*

test:
go test -i $(TEST) || exit 1
Expand Down
2 changes: 1 addition & 1 deletion examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ terraform {
}

provider "xsoar" {
main_host = "your.main.hostname.fqdn"
main_host = "https://your.main.hostname.fqdn"
api_key = "your_api_key"
insecure = true
}
Expand Down
5 changes: 5 additions & 0 deletions xsoar/data_source_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func (r dataSourceAccountType) GetSchema(_ context.Context) (tfsdk.Schema, diag.
Computed: true,
Optional: false,
},
"id": {
Type: types.StringType,
Computed: true,
},
},
}, nil
}
Expand Down Expand Up @@ -119,6 +123,7 @@ func (r dataSourceAccount) Read(ctx context.Context, req tfsdk.ReadDataSourceReq
HostGroupId: types.String{Value: account["hostGroupId"].(string)},
PropagationLabels: propagationLabels,
AccountRoles: roles,
Id: types.String{Value: account["id"].(string)},
}

// Set state
Expand Down
72 changes: 72 additions & 0 deletions xsoar/data_source_account_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,73 @@
package xsoar

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"strings"
"testing"
)

func TestAccAccountDataSource_basic(t *testing.T) {
rName := acctest.RandStringFromCharSet(5, acctest.CharSetAlpha)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccAccountDataSourcePreCheck(t) },
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"xsoar": func() (tfprotov6.ProviderServer, error) {
return tfsdk.NewProtocol6Server(New()), nil
},
},
CheckDestroy: testAccCheckAccountDataSourceDestroy(rName),
Steps: []resource.TestStep{
{
Config: testAccAccountDataSourceBasic(rName),
Check: resource.TestCheckResourceAttrPair("data.xsoar_account."+rName, "id", "xsoar_account."+rName, "id"),
},
},
})
}

func testAccAccountDataSourcePreCheck(t *testing.T) {}

func testAccCheckAccountDataSourceDestroy(r string) resource.TestCheckFunc {
return func(state *terraform.State) error {
rs, ok := state.RootModule().Resources["xsoar_account."+r]
if !ok {
return fmt.Errorf("not found: %s in %s", r, state.RootModule().Resources)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}

resp, _, err := openapiClient.DefaultApi.GetAccount(context.Background(), "acc_"+r).Execute()
if err != nil {
return fmt.Errorf("Error getting account: " + err.Error())
}
if resp != nil {
return fmt.Errorf("account returned when it should be destroyed")
}
return nil
}
}

func testAccAccountDataSourceBasic(name string) string {
c := `
resource "xsoar_account" "{name}" {
name = "{name}"
host_group_name = ""
account_roles = ["Administrator"]
propagation_labels = [""]
}
data "xsoar_account" "{name}" {
name = xsoar_account.{name}.name
}
`
c = strings.Replace(c, "{name}", name, -1)
return c
}
68 changes: 68 additions & 0 deletions xsoar/data_source_ha_group_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,69 @@
package xsoar

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"strings"
"testing"
)

func TestAccHAGroupDataSource_basic(t *testing.T) {
rName := acctest.RandStringFromCharSet(5, acctest.CharSetAlpha)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccHAGroupDataSourcePreCheck(t) },
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"xsoar": func() (tfprotov6.ProviderServer, error) {
return tfsdk.NewProtocol6Server(New()), nil
},
},
CheckDestroy: testAccCheckHAGroupDataSourceDestroy(rName),
Steps: []resource.TestStep{
{
Config: testAccHAGroupDataSourceBasic(rName),
Check: resource.TestCheckResourceAttrPair("data.xsoar_ha_group."+rName, "id", "xsoar_ha_group."+rName, "id"),
},
},
})
}

func testAccHAGroupDataSourcePreCheck(t *testing.T) {}

func testAccCheckHAGroupDataSourceDestroy(r string) resource.TestCheckFunc {
return func(state *terraform.State) error {
rs, ok := state.RootModule().Resources["xsoar_ha_group."+r]
if !ok {
return fmt.Errorf("not found: %s in %s", r, state.RootModule().Resources)
}

if rs.Primary.ID == "" {
return fmt.Errorf("no ID is set")
}

_, _, err := openapiClient.DefaultApi.GetHAGroup(context.Background(), r).Execute()
if err != nil {
return nil
}
return fmt.Errorf("found HA group when none was expected: " + err.Error())
}
}

func testAccHAGroupDataSourceBasic(name string) string {
c := `
resource "xsoar_ha_group" "{name}" {
name = "{name}"
elasticsearch_url = "http://elastic.xsoar.local:9200"
elastic_index_prefix = "{name}_"
}
data "xsoar_ha_group" "{name}" {
name = xsoar_ha_group.{name}.name
}
`
c = strings.Replace(c, "{name}", name, -1)
return c
}
52 changes: 25 additions & 27 deletions xsoar/data_source_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,27 @@ func (r dataSourceHostType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Dia
"ha_group_name": {
Type: types.StringType,
Computed: true,
Optional: false,
Optional: true,
},
"elasticearch_url": {
"elasticsearch_url": {
Type: types.StringType,
Computed: true,
Optional: false,
Optional: true,
},
"server_url": {
Type: types.StringType,
Computed: true,
Optional: false,
Computed: false,
Optional: true,
},
"ssh_user": {
Type: types.StringType,
Computed: true,
Optional: false,
Computed: false,
Optional: true,
},
"ssh_key_file": {
Type: types.StringType,
Computed: true,
Optional: false,
Computed: false,
Optional: true,
},
},
}, nil
Expand Down Expand Up @@ -101,14 +101,8 @@ func (r dataSourceHost) Read(ctx context.Context, req tfsdk.ReadDataSourceReques
// Map response body to resource schema attribute
var hostName = host["host"].(string)
var hostId = host["id"].(string)
var haGroupId = host["hostGroupId"].(string)

var result Host
result = Host{
Name: types.String{Value: hostName},
Id: types.String{Value: hostId},
}

haGroupId := host["hostGroupId"].(string)
haGroup, _, err := r.p.client.DefaultApi.GetHAGroup(ctx, haGroupId).Execute()
if err != nil {
resp.Diagnostics.AddError(
Expand All @@ -118,28 +112,32 @@ func (r dataSourceHost) Read(ctx context.Context, req tfsdk.ReadDataSourceReques
return
}

var result Host
result = Host{
Name: types.String{Value: hostName},
Id: types.String{Value: hostId},
}

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

var isElastic = false
if len(host["elasticsearchAddress"].(string)) > 0 {
isElastic = true
}
if isElastic {
var elasticsearchAddress string
elasticsearchAddress = host["elasticsearchAddress"].(string)
if !isHA || (!isHA && len(state.ElasticsearchUrl.Value) > 0) {
result.ElasticsearchUrl.Value = elasticsearchAddress
if isHA {
result.ElasticsearchUrl.Null = true
} else {
result.ElasticsearchUrl.Value = host["elasticsearchAddress"].(string)
}
} else {
result.ElasticsearchUrl.Null = true
}

// Set state
diags = resp.State.Set(ctx, &state)
diags = resp.State.Set(ctx, result)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand Down
Loading

0 comments on commit 18a99b4

Please sign in to comment.