Skip to content

Commit

Permalink
feat: Add support for import of cloudfoundry_domain (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
ANUGRAHG authored Nov 19, 2024
1 parent b93982c commit ebbf9ee
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 5 deletions.
68 changes: 68 additions & 0 deletions pkg/tfimportprovider/cloudFoundryDomainImportProvider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package tfimportprovider

import (
"fmt"
"log"
"slices"
"strings"

tfutils "github.com/SAP/terraform-exporter-btp/pkg/tfutils"
)

type cloudfoundryDomainImportProvider struct {
TfImportProvider
}

func newCloudfoundryDomainImportProvider() ITfImportProvider {
return &cloudfoundryDomainImportProvider{
TfImportProvider: TfImportProvider{
resourceType: tfutils.CfDomainType,
},
}
}
func (tf *cloudfoundryDomainImportProvider) GetImportBlock(data map[string]interface{}, levelId string, filterValues []string) (string, int, error) {
count := 0
orgId := levelId
resourceDoc, err := tfutils.GetDocByResourceName(tfutils.ResourcesKind, tfutils.CfDomainType, tfutils.OrganizationLevel)
if err != nil {
fmt.Print("\r\n")
log.Fatalf("read doc failed!")
return "", count, err
}
importBlock, count, err := createDomainImportBlock(data, orgId, filterValues, resourceDoc)
if err != nil {
return "", count, err
}
return importBlock, count, nil
}
func createDomainImportBlock(data map[string]interface{}, orgId string, filterValues []string, resourceDoc tfutils.EntityDocs) (importBlock string, count int, err error) {
count = 0
domains := data["domains"].([]interface{})
if len(filterValues) != 0 {
var cfAllDomains []string
for x, value := range domains {
domain := value.(map[string]interface{})
cfAllDomains = append(cfAllDomains, fmt.Sprintf("%v", domain["name"]))
if slices.Contains(filterValues, fmt.Sprintf("%v", domain["name"])) {
importBlock += templateDomainImport(x, domain, resourceDoc)
count++
}
}
missingSpace, subset := isSubset(cfAllDomains, filterValues)
if !subset {
return "", 0, fmt.Errorf("cloud foudndry domain %s not found in the organization with ID %s. Please adjust it in the provided file", missingSpace, orgId)
}
} else {
for x, value := range domains {
domain := value.(map[string]interface{})
importBlock += templateDomainImport(x, domain, resourceDoc)
count++
}
}
return importBlock, count, nil
}
func templateDomainImport(x int, domain map[string]interface{}, resourceDoc tfutils.EntityDocs) string {
template := strings.Replace(resourceDoc.Import, "<resource_name>", "domain_"+fmt.Sprintf("%v", x), -1)
template = strings.Replace(template, "<domain_guid>", fmt.Sprintf("%v", domain["id"]), -1)
return template + "\n"
}
72 changes: 72 additions & 0 deletions pkg/tfimportprovider/cloudFoundryDomainImportProvider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package tfimportprovider

import (
"testing"

tfutils "github.com/SAP/terraform-exporter-btp/pkg/tfutils"
"github.com/stretchr/testify/assert"
)

func TestCreateCfDomainImportBlock(t *testing.T) {
resourceDoc := tfutils.EntityDocs{
Import: "import {\n\t\t\t\tto = cloudfoundry_domain.<resource_name>\n\t\t\t\tid = \"<domain_guid>\"\"\n\t\t\t }\n",
}

jsonString := "{\"domains\": [{\"annotations\": null,\"created_at\":\"2022-01-02T08:56:47Z\",\"id\":\"23456\",\"internal\": false,\"labels\": null,\"name\":\"test-domain1.com\",\"org\":\"12345\",\"router_group\": null,\"shared_orgs\": null,\"supported_protocols\": [\"http\"],\"updated_at\":\"2023-01-02T08:56:47Z\"}],\"org\":\"12345\"}"
dataDomain, _ := GetDataFromJsonString(jsonString)

jsonStringMultipleDomains := "{\"domains\": [{\"annotations\": null,\"created_at\":\"2022-01-02T08:56:47Z\",\"id\":\"23456\",\"internal\": false,\"labels\": null,\"name\":\"test-domain1.com\",\"org\":\"12345\",\"router_group\": null,\"shared_orgs\": null,\"supported_protocols\": [\"http\"],\"updated_at\":\"2023-01-02T08:56:47Z\"}, {\"annotations\": null,\"created_at\":\"2022-01-02T09:29:59Z\",\"id\":\"34567\",\"internal\": false,\"labels\": null,\"name\":\"test-domain2.com\",\"org\":\"12345\",\"router_group\": null,\"shared_orgs\": null,\"supported_protocols\": [\"http\"],\"updated_at\":\"2023-01-02T09:29:59Z\"}],\"org\":\"12345\"}"
dataMultipleDomains, _ := GetDataFromJsonString(jsonStringMultipleDomains)

tests := []struct {
name string
data map[string]interface{}
orgId string
filterValues []string
expectedBlock string
expectedCount int
expectError bool
}{

{
name: "Valid data without filter",
data: dataDomain,
orgId: "12345",
filterValues: []string{},
expectedBlock: "import {\n\t\t\t\tto = cloudfoundry_domain.domain_0\n\t\t\t\tid = \"23456\"\"\n\t\t\t }\n\n",
expectedCount: 1,
expectError: false,
},
{
name: "Valid data with matching filter",
data: dataMultipleDomains,
orgId: "12345",
filterValues: []string{"test-domain1.com"},
expectedBlock: "import {\n\t\t\t\tto = cloudfoundry_domain.domain_0\n\t\t\t\tid = \"23456\"\"\n\t\t\t }\n\n",
expectedCount: 1,
expectError: false,
},
{
name: "Invalid filter value",
data: dataDomain,
orgId: "12345",
filterValues: []string{"wrong-domain-name"},
expectedBlock: "",
expectedCount: 0,
expectError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
importBlock, count, err := createDomainImportBlock(tt.data, tt.orgId, tt.filterValues, resourceDoc)
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expectedBlock, importBlock)
assert.Equal(t, tt.expectedCount, count)
}
})
}
}
2 changes: 2 additions & 0 deletions pkg/tfimportprovider/tfImportProviderFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func GetImportBlockProvider(cmdResourceName string, level string) (ITfImportProv
return newcloudfoundrySpaceImportProvider(), nil
case tfutils.CmdCfUserParameter:
return newcloudfoundryUserImportProvider(), nil
case tfutils.CmdCfDomainParamater:
return newCloudfoundryDomainImportProvider(), nil
default:
return nil, fmt.Errorf("unsupported resource provided")
}
Expand Down
1 change: 1 addition & 0 deletions pkg/tfutils/tfConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var AllowedResourcesDirectory = []string{
var AllowedResourcesOrganization = []string{
CmdCfSpaceParameter,
CmdCfUserParameter,
CmdCfDomainParamater,
}

func GenerateConfig(resourceFileName string, configFolder string, isMainCmd bool, resourceNameLong string) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/tfutils/tfDocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ func cleanupDocument(name string, doc EntityDocs) (EntityDocs, bool) {
for attrName, attrValue := range cleanedAttributes {
doc.Import += fmt.Sprintf("%s = \"%s\" \n", attrName, attrValue)
}
} else if name == "cloudfoundry_users" {
} else if name == "cloudfoundry_users" || name == "cloudfoundry_domains" {
doc.Import += "org = \"The ID of the organization\" \n"
} else {
doc.Import += "id = \"The ID of the subaccount\" \n"
Expand Down
14 changes: 10 additions & 4 deletions pkg/tfutils/tfImport.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

// Constants for TF version for Terraform providers e.g. for SAP BTP
const BtpProviderVersion = "v1.8.0"
const CfProviderVersion = "v1.0.0"
const CfProviderVersion = "v1.1.0"

const (
SubaccountLevel = "subaccountLevel"
Expand All @@ -41,6 +41,7 @@ const (
CmdSecuritySettingParameter string = "security-settings"
CmdCfSpaceParameter string = "spaces"
CmdCfUserParameter string = "users"
CmdCfDomainParamater string = "domains"
)

const (
Expand All @@ -64,8 +65,9 @@ const (
)

const (
CfSpaceType string = "cloudfoundry_space"
CfUserType string = "cloudfoundry_user"
CfSpaceType string = "cloudfoundry_space"
CfUserType string = "cloudfoundry_user"
CfDomainType string = "cloudfoundry_domain"
)

const DirectoryFeatureDefault string = "DEFAULT"
Expand Down Expand Up @@ -179,6 +181,8 @@ func TranslateResourceParamToTechnicalName(resource string, level string) string
return CfSpaceType
case CmdCfUserParameter:
return CfUserType
case CmdCfDomainParamater:
return CfDomainType
}
return ""
}
Expand Down Expand Up @@ -273,7 +277,7 @@ func readDataSource(subaccountId string, directoryId string, organizationId stri
dataBlock = strings.Replace(doc.Import, doc.Attributes["directory_id"], directoryId, -1)
}
case OrganizationLevel:
if resourceName == CfUserType {
if resourceName == CfUserType || resourceName == CfDomainType {
dataBlock = strings.Replace(doc.Import, "The ID of the organization", organizationId, -1)
} else {
dataBlock = strings.Replace(doc.Import, doc.Attributes["org"], organizationId, -1)
Expand Down Expand Up @@ -367,6 +371,8 @@ func transformDataToStringArray(btpResource string, data map[string]interface{})
transformDataToStringArrayGeneric(data, &stringArr, "spaces", "name")
case CfUserType:
transformDataToStringArrayGeneric(data, &stringArr, "users", "username")
case CfDomainType:
transformDataToStringArrayGeneric(data, &stringArr, "domains", "name")
}
return stringArr
}
Expand Down

0 comments on commit ebbf9ee

Please sign in to comment.