generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for import of cloudfoundry_domain (#184)
- Loading branch information
Showing
6 changed files
with
154 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
72
pkg/tfimportprovider/cloudFoundryDomainImportProvider_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters