Skip to content

Commit

Permalink
chore(refactor): code styling (#852)
Browse files Browse the repository at this point in the history
  • Loading branch information
v0lkc authored Jul 16, 2024
1 parent e2c53e5 commit 405ec90
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 58 deletions.
12 changes: 6 additions & 6 deletions internal/btpcli/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,21 +287,21 @@ func TestV2Client_BrowserLogin(t *testing.T) {
srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
_, _ = w.Write([]byte("{\"loginId\":\"login.id\",\"subdomainRequired\":false}"))
} else if strings.Contains(r.URL.Path, "/login.id"){
} else if strings.Contains(r.URL.Path, "/login.id") {

res, _ := io.ReadAll(r.Body)

idp := strings.Split(strings.Split(string(res), ",")[0], ":")[1]
if idp == "\"\"" {
idp = "accounts.sap.com"
} else {
idp = "customidp.accounts.ondemand.com"
}

w.Header().Add("HeaderCLISessionId", "sessionid")
w.WriteHeader(http.StatusOK)

_, _ = w.Write([]byte("{\"issuer\":\""+idp+"\",\"refreshToken\":\"sessionid\",\"user\":\"john.doe\",\"mail\":\"[email protected]\"}"))
_, _ = w.Write([]byte("{\"issuer\":\"" + idp + "\",\"refreshToken\":\"sessionid\",\"user\":\"john.doe\",\"mail\":\"[email protected]\"}"))
} else {
w.WriteHeader(http.StatusForbidden)
}
Expand Down Expand Up @@ -334,15 +334,15 @@ func TestV2Client_BrowserLogin(t *testing.T) {
},
}, uut.session)
})

t.Run("happy path - with custom idp", func(t *testing.T) {
uut := NewV2ClientWithHttpClient(srv.Client(), srvUrl)
uut.session = &Session{}
uut.newCorrelationID = func() string {
return "fake-correlation-id"
}
_, err := uut.BrowserLogin(context.TODO(), &BrowserLoginRequest{
CustomIdp: "my.custom.idp",
CustomIdp: "my.custom.idp",
GlobalAccountSubdomain: "my-subdomain",
})

Expand Down
5 changes: 2 additions & 3 deletions internal/btpcli/clienttypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,15 @@ type LoginResponse struct {
type BrowserLoginPostResponse struct {
Issuer string `json:"issuer"`
RefreshToken string `json:"refreshToken"`
Username string `json:"user"`
Email string `json:"mail"`
Username string `json:"user"`
Email string `json:"mail"`
}

type BrowserResponse struct {
LoginID string `json:"loginId"`
SubdomainRequired bool `json:"subdomainRequired"`
}


/* Command */
func NewCommandRequest(action Action, command string, args any) *CommandRequest {
return &CommandRequest{
Expand Down
6 changes: 3 additions & 3 deletions internal/btpcli/facade_accounts_entitlement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestAccountsEntitlementFacade_AssignToSubaccount(t *testing.T) {

assertCall(t, r, command, ActionAssign, map[string]string{
"globalAccount": "795b53bb-a3f0-4769-adf0-26173282a975",
"directoryID": directoryId,
"directoryID": directoryId,
"subaccount": subaccountId,
"serviceName": serviceName,
"servicePlanName": planName,
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestAccountsEntitlementFacade_EnableInSubaccount(t *testing.T) {

assertCall(t, r, command, ActionAssign, map[string]string{
"globalAccount": "795b53bb-a3f0-4769-adf0-26173282a975",
"directoryID": directoryId,
"directoryID": directoryId,
"subaccount": subaccountId,
"serviceName": serviceName,
"servicePlanName": planName,
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestAccountsEntitlementFacade_DisableInSubaccount(t *testing.T) {

assertCall(t, r, command, ActionAssign, map[string]string{
"globalAccount": "795b53bb-a3f0-4769-adf0-26173282a975",
"directoryID": directoryId,
"directoryID": directoryId,
"subaccount": subaccountId,
"serviceName": serviceName,
"servicePlanName": planName,
Expand Down
2 changes: 1 addition & 1 deletion internal/btpcli/facade_accounts_global_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ func (f *accountsGlobalAccountFacade) GetWithHierarchy(ctx context.Context) (cis
"globalAccount": f.cliClient.GetGlobalAccountSubdomain(),
"showHierarchy": "true",
}))
}
}
6 changes: 3 additions & 3 deletions internal/btpcli/facade_services_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ func (f servicesInstanceFacade) Update(ctx context.Context, args *ServiceInstanc
}

type ServiceInstanceShareInput struct {
Id string `btpcli:"id"`
Subaccount string `btpcli:"subaccount"`
Name string `btpcli:"name"`
Id string `btpcli:"id"`
Subaccount string `btpcli:"subaccount"`
Name string `btpcli:"name"`
}

func (f servicesInstanceFacade) Share(ctx context.Context, args *ServiceInstanceShareInput) (servicemanager.ServiceInstanceResponseObject, CommandResponse, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestDataSourceGlobalAccountSecuritySettings(t *testing.T) {
func TestDataSourceGlobalaccountSecuritySettings(t *testing.T) {

t.Parallel()
t.Run("happy path - security settings by id", func(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions internal/provider/datasource_globalaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestDataSourceGlobalAccount(t *testing.T) {
func TestDataSourceGlobalaccount(t *testing.T) {
t.Parallel()
t.Run("happy path", func(t *testing.T) {
rec, user := setupVCR(t, "fixtures/datasource_globalaccount")
Expand All @@ -22,7 +22,7 @@ func TestDataSourceGlobalAccount(t *testing.T) {
ProtoV6ProviderFactories: getProviders(rec.GetDefaultClient()),
Steps: []resource.TestStep{
{
Config: hclProviderFor(user) + hclDatasourceGlobalAccount("uut"),
Config: hclProviderFor(user) + hclDatasourceGlobalaccount("uut"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.btp_globalaccount.uut", "id", "03760ecf-9d89-4189-a92a-1c7efed09298"),
resource.TestCheckResourceAttr("data.btp_globalaccount.uut", "name", "terraform-integration-canary"),
Expand Down Expand Up @@ -53,14 +53,14 @@ func TestDataSourceGlobalAccount(t *testing.T) {
ProtoV6ProviderFactories: getProviders(srv.Client()),
Steps: []resource.TestStep{
{
Config: hclProviderForCLIServerAt(srv.URL) + hclDatasourceGlobalAccount("uut"),
Config: hclProviderForCLIServerAt(srv.URL) + hclDatasourceGlobalaccount("uut"),
ExpectError: regexp.MustCompile(`received response with unexpected status \[Status: 404; Correlation ID:\s+[a-f0-9\-]+\]`),
},
},
})
})
}

func hclDatasourceGlobalAccount(resourceName string) string {
func hclDatasourceGlobalaccount(resourceName string) string {
return fmt.Sprintf(`data "btp_globalaccount" "%s" {}`, resourceName)
}
8 changes: 4 additions & 4 deletions internal/provider/datasource_globalaccount_with_hierarchy.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,9 @@ __Further documentation:__

func (ds *globalaccountWithHierarchyDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {

var globalAccountData globalAccountHierarchyType
var globalaccountData globalaccountHierarchyType

diags := req.Config.Get(ctx, &globalAccountData)
diags := req.Config.Get(ctx, &globalaccountData)

resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -411,9 +411,9 @@ func (ds *globalaccountWithHierarchyDataSource) Read(ctx context.Context, req da
return
}

globalAccountData, diags = globalAccountHierarchyValueFrom(ctx, cliRes)
globalaccountData, diags = globalaccountHierarchyValueFrom(ctx, cliRes)
resp.Diagnostics.Append(diags...)

diags = resp.State.Set(ctx, &globalAccountData)
diags = resp.State.Set(ctx, &globalaccountData)
resp.Diagnostics.Append(diags...)
}
22 changes: 11 additions & 11 deletions internal/provider/resource_globalaccount_role_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type globalaccountRoleCollectionRoleRefTestType struct {
RoleTemplateName string `json:"role_template_name"`
}

func TestResourceGlobalAccountRoleCollection(t *testing.T) {
func TestResourceGlobalaccountRoleCollection(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
rec, user := setupVCR(t, "fixtures/resource_globalaccount_role_collection")
defer stopQuietly(rec)
Expand All @@ -26,7 +26,7 @@ func TestResourceGlobalAccountRoleCollection(t *testing.T) {
ProtoV6ProviderFactories: getProviders(rec.GetDefaultClient()),
Steps: []resource.TestStep{
{
Config: hclProviderFor(user) + hclResourceGlobalAccountRoleCollection(
Config: hclProviderFor(user) + hclResourceGlobalaccountRoleCollection(
"uut",
"My new role collection",
"Description of my new role collection",
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestResourceGlobalAccountRoleCollection(t *testing.T) {
ProtoV6ProviderFactories: getProviders(rec.GetDefaultClient()),
Steps: []resource.TestStep{
{
Config: hclProviderFor(user) + hclResourceGlobalAccountRoleCollection(
Config: hclProviderFor(user) + hclResourceGlobalaccountRoleCollection(
"uut",
"My new role collection",
"Description of my new role collection",
Expand All @@ -77,7 +77,7 @@ func TestResourceGlobalAccountRoleCollection(t *testing.T) {
),
},
{
Config: hclProviderFor(user) + hclResourceGlobalAccountRoleCollection(
Config: hclProviderFor(user) + hclResourceGlobalaccountRoleCollection(
"uut",
"My new role collection",
"Description of my updated role collection",
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestResourceGlobalAccountRoleCollection(t *testing.T) {
ProtoV6ProviderFactories: getProviders(rec.GetDefaultClient()),
Steps: []resource.TestStep{
{
Config: hclProviderFor(user) + hclResourceGlobalAccountRoleCollection(
Config: hclProviderFor(user) + hclResourceGlobalaccountRoleCollection(
"uut",
"My new role collection",
"Description of my new role collection",
Expand All @@ -129,7 +129,7 @@ func TestResourceGlobalAccountRoleCollection(t *testing.T) {
),
},
{
Config: hclProviderFor(user) + hclResourceGlobalAccountRoleCollection(
Config: hclProviderFor(user) + hclResourceGlobalaccountRoleCollection(
"uut",
"My new role collection",
"",
Expand Down Expand Up @@ -164,7 +164,7 @@ func TestResourceGlobalAccountRoleCollection(t *testing.T) {
ProtoV6ProviderFactories: getProviders(rec.GetDefaultClient()),
Steps: []resource.TestStep{
{
Config: hclProviderFor(user) + hclResourceGlobalAccountRoleCollection(
Config: hclProviderFor(user) + hclResourceGlobalaccountRoleCollection(
"uut",
"My new role collection",
"Description of my new role collection",
Expand All @@ -181,7 +181,7 @@ func TestResourceGlobalAccountRoleCollection(t *testing.T) {
),
},
{
Config: hclProviderFor(user) + hclResourceGlobalAccountRoleCollectionWoDescription(
Config: hclProviderFor(user) + hclResourceGlobalaccountRoleCollectionWoDescription(
"uut",
"My new role collection",
globalaccountRoleCollectionRoleRefTestType{
Expand Down Expand Up @@ -215,7 +215,7 @@ func TestResourceGlobalAccountRoleCollection(t *testing.T) {
ProtoV6ProviderFactories: getProviders(rec.GetDefaultClient()),
Steps: []resource.TestStep{
{
Config: hclProviderFor(user) + hclResourceGlobalAccountRoleCollection("uut", "My new role collection", "Description of my new role collection"),
Config: hclProviderFor(user) + hclResourceGlobalaccountRoleCollection("uut", "My new role collection", "Description of my new role collection"),
},
{
ResourceName: "btp_globalaccount_role_collection.uut",
Expand All @@ -229,7 +229,7 @@ func TestResourceGlobalAccountRoleCollection(t *testing.T) {
})
}

func hclResourceGlobalAccountRoleCollection(resourceName string, displayName string, description string, roles ...globalaccountRoleCollectionRoleRefTestType) string {
func hclResourceGlobalaccountRoleCollection(resourceName string, displayName string, description string, roles ...globalaccountRoleCollectionRoleRefTestType) string {
if roles == nil {
roles = []globalaccountRoleCollectionRoleRefTestType{}
}
Expand All @@ -242,7 +242,7 @@ func hclResourceGlobalAccountRoleCollection(resourceName string, displayName str
}`, resourceName, displayName, description, string(rolesJson))
}

func hclResourceGlobalAccountRoleCollectionWoDescription(resourceName string, displayName string, roles ...globalaccountRoleCollectionRoleRefTestType) string {
func hclResourceGlobalaccountRoleCollectionWoDescription(resourceName string, displayName string, roles ...globalaccountRoleCollectionRoleRefTestType) string {
if roles == nil {
roles = []globalaccountRoleCollectionRoleRefTestType{}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestResourceRolCollectionAssignment(t *testing.T) {
func TestResourceSubaccountRoleCollectionAssignment(t *testing.T) {
t.Parallel()
t.Run("happy path - simple role collection assignment", func(t *testing.T) {
rec, user := setupVCR(t, "fixtures/resource_subaccount_role_collection_assignment")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type subaccountRoleCollectionRoleRefTestType struct {
RoleTemplateName string `json:"role_template_name"`
}

func TestResourceSubAccountRoleCollection(t *testing.T) {
func TestResourceSubaccountRoleCollection(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
rec, user := setupVCR(t, "fixtures/resource_subaccount_role_collection")
defer stopQuietly(rec)
Expand Down
20 changes: 10 additions & 10 deletions internal/provider/type_globalaccount_hierarchy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
)

type globalAccountHierarchyType struct {
type globalaccountHierarchyType struct {
ID types.String `tfsdk:"id"`
CreatedDate types.String `tfsdk:"created_date"`
Directories types.List `tfsdk:"directories"`
Expand All @@ -20,8 +20,8 @@ type globalAccountHierarchyType struct {
Type types.String `tfsdk:"type"`
}

func globalAccountHierarchyValueFrom(ctx context.Context, value cis.GlobalAccountResponseObject) (globalAccountHierarchyType, diag.Diagnostics) {
globalAccount := globalAccountHierarchyType{
func globalaccountHierarchyValueFrom(ctx context.Context, value cis.GlobalAccountResponseObject) (globalaccountHierarchyType, diag.Diagnostics) {
globalaccount := globalaccountHierarchyType{
ID: types.StringValue(value.Guid),
CreatedDate: timeToValue(value.CreatedDate.Time()),
ModifiedDate: timeToValue(value.ModifiedDate.Time()),
Expand All @@ -35,21 +35,21 @@ func globalAccountHierarchyValueFrom(ctx context.Context, value cis.GlobalAccoun

if len(value.Children) > 0 {
//The dirctory level is mentioned as 6 inorder to align with the schema strcuture defined as per the provider.
directories, diags := directoriesHierarchyValueFrom(ctx, value.Children, globalAccount.Name, globalAccount.Type, 6)
directories, diags := directoriesHierarchyValueFrom(ctx, value.Children, globalaccount.Name, globalaccount.Type, 6)
summary.Append(diags...)
globalAccount.Directories, diags = types.ListValueFrom(ctx, directoryObjectType(6), directories)
globalaccount.Directories, diags = types.ListValueFrom(ctx, directoryObjectType(6), directories)
summary.Append(diags...)
} else {
globalAccount.Directories = types.ListNull(directoryObjectType(6))
globalaccount.Directories = types.ListNull(directoryObjectType(6))
}

if len(value.Subaccounts) > 0 {
subaccounts := subaccountsHierarchyValueFrom(ctx, value.Subaccounts, globalAccount.Name, globalAccount.Type)
globalAccount.Subaccounts, diags = types.ListValueFrom(ctx, subaccountObjectType, subaccounts)
subaccounts := subaccountsHierarchyValueFrom(ctx, value.Subaccounts, globalaccount.Name, globalaccount.Type)
globalaccount.Subaccounts, diags = types.ListValueFrom(ctx, subaccountObjectType, subaccounts)
summary.Append(diags...)
} else {
globalAccount.Subaccounts = types.ListNull(subaccountObjectType)
globalaccount.Subaccounts = types.ListNull(subaccountObjectType)
}

return globalAccount, summary
return globalaccount, summary
}
16 changes: 6 additions & 10 deletions internal/provider/type_subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,12 @@ func subaccountValueFrom(ctx context.Context, value cis.SubaccountResponseObject
return subaccount, diagnostics
}

func determineParentIdByFeature(cli *btpcli.ClientFacade, ctx context.Context, parentIdToVerify string, featureType string) (parentId string, isParentGlobalAccount bool) {

parentId = ""
isParentGlobalAccount = false

func determineParentIdByFeature(cli *btpcli.ClientFacade, ctx context.Context, parentIdToVerify string, featureType string) (parentId string, isParentGlobalaccount bool) {
parentData, _, err := cli.Accounts.Directory.Get(ctx, parentIdToVerify)

// The parent is the global account
if err != nil {
isParentGlobalAccount = true
isParentGlobalaccount = true
return
}

Expand All @@ -75,26 +71,26 @@ func determineParentIdByFeature(cli *btpcli.ClientFacade, ctx context.Context, p
parentId = parentIdToVerify
} else {
// Parent is a directory, but not with entitlements feature enabled -> step up the hierarchy
parentId, isParentGlobalAccount = determineParentIdByFeature(cli, ctx, parentData.ParentGUID, featureType)
parentId, isParentGlobalaccount = determineParentIdByFeature(cli, ctx, parentData.ParentGUID, featureType)
}

return
}

func hasFeature(features []string, featureType string) (featureTypeFound bool) {

for _, f := range features {
if f == featureType {
featureTypeFound = true
}
}

return
}

func determineParentIdForEntitlement(cli *btpcli.ClientFacade, ctx context.Context, parentIdToVerify string) (parentId string, isParentGlobalAccount bool) {
func determineParentIdForEntitlement(cli *btpcli.ClientFacade, ctx context.Context, parentIdToVerify string) (parentId string, isParentGlobalaccount bool) {
return determineParentIdByFeature(cli, ctx, parentIdToVerify, EntitlementFeature)
}

func determineParentIdForAuthorization(cli *btpcli.ClientFacade, ctx context.Context, parentIdToVerify string) (parentId string, isParentGlobalAccount bool) {
func determineParentIdForAuthorization(cli *btpcli.ClientFacade, ctx context.Context, parentIdToVerify string) (parentId string, isParentGlobalaccount bool) {
return determineParentIdByFeature(cli, ctx, parentIdToVerify, AuthorizationFeature)
}

0 comments on commit 405ec90

Please sign in to comment.