diff --git a/pkg/persistence/account/loader/load.go b/pkg/persistence/account/loader/load.go index 8fc51e1e8..209324dfc 100644 --- a/pkg/persistence/account/loader/load.go +++ b/pkg/persistence/account/loader/load.go @@ -45,7 +45,7 @@ func Load(fs afero.Fs, rootPath string) (*account.Resources, error) { return nil, fmt.Errorf("account management resources from %q are invalid: %w", rootPath, err) } - return transform(persisted), nil + return transformToAccountResources(persisted), nil } // HasAnyAccountKeyDefined checks whether the map has any AM key defined. @@ -131,28 +131,6 @@ func loadFile(fs afero.Fs, yamlFilePath string) (*persistence.File, error) { return &file, err } -func validateFile(file persistence.File) error { - for _, p := range file.Policies { - if err := validatePolicy(p); err != nil { - return err - } - } - - for _, g := range file.Groups { - if err := validateGroup(g); err != nil { - return err - } - } - - for _, u := range file.Users { - if err := validateUser(u); err != nil { - return err - } - } - - return nil -} - func addResourcesFromFile(res persistence.Resources, file persistence.File) error { for _, p := range file.Policies { if _, exists := res.Policies[p.ID]; exists { @@ -177,115 +155,3 @@ func addResourcesFromFile(res persistence.Resources, file persistence.File) erro return nil } - -func transform(resources *persistence.Resources) *account.Resources { - return &account.Resources{ - Policies: transformPolicies(resources.Policies), - Groups: transformGroups(resources.Groups), - Users: transformUsers(resources.Users), - } -} - -func transformPolicies(in map[string]persistence.Policy) map[account.PolicyId]account.Policy { - policies := make(map[account.PolicyId]account.Policy, len(in)) - for id, v := range in { - policies[id] = account.Policy{ - ID: v.ID, - Name: v.Name, - Level: transformLevel(v.Level), - Description: v.Description, - Policy: v.Policy, - OriginObjectID: v.OriginObjectID, - } - } - return policies -} - -func transformLevel(level persistence.PolicyLevel) any { - switch level.Type { - case persistence.PolicyLevelAccount: - return account.PolicyLevelAccount{Type: level.Type} - case persistence.PolicyLevelEnvironment: - return account.PolicyLevelEnvironment{Type: level.Type, Environment: level.Environment} - default: - panic("unable to convert persistence model") - } -} - -func transformGroups(in map[string]persistence.Group) map[account.GroupId]account.Group { - groups := make(map[account.GroupId]account.Group, len(in)) - for id, v := range in { - groups[id] = account.Group{ - ID: v.ID, - Name: v.Name, - Description: v.Description, - FederatedAttributeValues: v.FederatedAttributeValues, - Account: transformAccount(v.Account), - Environment: transformEnvironments(v.Environment), - ManagementZone: transformManagementZones(v.ManagementZone), - OriginObjectID: v.OriginObjectID, - } - } - return groups -} - -func transformAccount(in *persistence.Account) *account.Account { - if in == nil { - return nil - } - - return &account.Account{ - Permissions: in.Permissions, - Policies: transformReferences(in.Policies), - } -} - -func transformEnvironments(in []persistence.Environment) []account.Environment { - env := make([]account.Environment, len(in)) - for i, e := range in { - env[i] = account.Environment{ - Name: e.Name, - Permissions: e.Permissions, - Policies: transformReferences(e.Policies), - } - } - return env -} - -func transformManagementZones(in []persistence.ManagementZone) []account.ManagementZone { - managementZones := make([]account.ManagementZone, len(in)) - for i, m := range in { - managementZones[i] = account.ManagementZone{ - Environment: m.Environment, - ManagementZone: m.ManagementZone, - Permissions: m.Permissions, - } - } - return managementZones -} - -func transformUsers(in map[string]persistence.User) map[account.UserId]account.User { - users := make(map[account.UserId]account.User, len(in)) - for id, v := range in { - users[id] = account.User{ - Email: v.Email, - Groups: transformReferences(v.Groups), - } - } - return users -} - -func transformReferences(in []persistence.Reference) []account.Ref { - res := make([]account.Ref, len(in)) - for i, el := range in { - switch el.Type { - case persistence.ReferenceType: - res[i] = account.Reference{Id: el.Id} - case "": - res[i] = account.StrReference(el.Value) - default: - panic("unable to convert persistence model") - } - } - return res -} diff --git a/pkg/persistence/account/loader/transform.go b/pkg/persistence/account/loader/transform.go new file mode 100644 index 000000000..9f984b1fd --- /dev/null +++ b/pkg/persistence/account/loader/transform.go @@ -0,0 +1,134 @@ +/* + * @license + * Copyright 2025 Dynatrace LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package loader + +import ( + "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/account" + persistence "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/persistence/account/internal/types" +) + +func transformToAccountResources(resources *persistence.Resources) *account.Resources { + return &account.Resources{ + Policies: transformPolicies(resources.Policies), + Groups: transformGroups(resources.Groups), + Users: transformUsers(resources.Users), + } +} + +func transformPolicies(pPolicies map[string]persistence.Policy) map[account.PolicyId]account.Policy { + policies := make(map[account.PolicyId]account.Policy, len(pPolicies)) + for id, v := range pPolicies { + policies[id] = account.Policy{ + ID: v.ID, + Name: v.Name, + Level: transformLevel(v.Level), + Description: v.Description, + Policy: v.Policy, + OriginObjectID: v.OriginObjectID, + } + } + return policies +} + +func transformLevel(pLevel persistence.PolicyLevel) any { + switch pLevel.Type { + case persistence.PolicyLevelAccount: + return account.PolicyLevelAccount{Type: pLevel.Type} + case persistence.PolicyLevelEnvironment: + return account.PolicyLevelEnvironment{Type: pLevel.Type, Environment: pLevel.Environment} + default: + panic("unable to convert persistence model") + } +} + +func transformGroups(pGroups map[string]persistence.Group) map[account.GroupId]account.Group { + groups := make(map[account.GroupId]account.Group, len(pGroups)) + for id, v := range pGroups { + groups[id] = account.Group{ + ID: v.ID, + Name: v.Name, + Description: v.Description, + FederatedAttributeValues: v.FederatedAttributeValues, + Account: transformAccount(v.Account), + Environment: transformEnvironments(v.Environment), + ManagementZone: transformManagementZones(v.ManagementZone), + OriginObjectID: v.OriginObjectID, + } + } + return groups +} + +func transformAccount(pAccount *persistence.Account) *account.Account { + if pAccount == nil { + return nil + } + + return &account.Account{ + Permissions: pAccount.Permissions, + Policies: transformReferences(pAccount.Policies), + } +} + +func transformEnvironments(pEnvironments []persistence.Environment) []account.Environment { + env := make([]account.Environment, len(pEnvironments)) + for i, e := range pEnvironments { + env[i] = account.Environment{ + Name: e.Name, + Permissions: e.Permissions, + Policies: transformReferences(e.Policies), + } + } + return env +} + +func transformManagementZones(pManagementZones []persistence.ManagementZone) []account.ManagementZone { + managementZones := make([]account.ManagementZone, len(pManagementZones)) + for i, m := range pManagementZones { + managementZones[i] = account.ManagementZone{ + Environment: m.Environment, + ManagementZone: m.ManagementZone, + Permissions: m.Permissions, + } + } + return managementZones +} + +func transformUsers(pUsers map[string]persistence.User) map[account.UserId]account.User { + users := make(map[account.UserId]account.User, len(pUsers)) + for id, v := range pUsers { + users[id] = account.User{ + Email: v.Email, + Groups: transformReferences(v.Groups), + } + } + return users +} + +func transformReferences(pReferences []persistence.Reference) []account.Ref { + res := make([]account.Ref, len(pReferences)) + for i, el := range pReferences { + switch el.Type { + case persistence.ReferenceType: + res[i] = account.Reference{Id: el.Id} + case "": + res[i] = account.StrReference(el.Value) + default: + panic("unable to convert persistence model") + } + } + return res +} diff --git a/pkg/persistence/account/loader/validate.go b/pkg/persistence/account/loader/validate.go index 47039f6d2..65e023f32 100644 --- a/pkg/persistence/account/loader/validate.go +++ b/pkg/persistence/account/loader/validate.go @@ -19,7 +19,9 @@ package loader import ( "errors" "fmt" + "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/persistence/account/internal/types" + persistence "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/persistence/account/internal/types" ) // validateReferences checks the references in the provided AMResources instance to ensure @@ -82,6 +84,28 @@ func policyExists(a *types.Resources, id string) bool { } +func validateFile(file persistence.File) error { + for _, p := range file.Policies { + if err := validatePolicy(p); err != nil { + return err + } + } + + for _, g := range file.Groups { + if err := validateGroup(g); err != nil { + return err + } + } + + for _, u := range file.Users { + if err := validateUser(u); err != nil { + return err + } + } + + return nil +} + func validateUser(u types.User) error { if u.Email == "" { return errors.New("missing required field 'email' for user")