Skip to content

Commit

Permalink
Merge pull request #389 from canonical/IAM-1016
Browse files Browse the repository at this point in the history
IAM 1016
  • Loading branch information
shipperizer authored Aug 29, 2024
2 parents bc3570e + 429591a commit 1cead53
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
33 changes: 24 additions & 9 deletions pkg/idp/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (s *Service) ListResources(ctx context.Context) ([]*Configuration, error) {
return nil, err
}

return s.idpConfiguration(cm.Data), nil
return s.idpConfiguration(cm.Data)

}

Expand All @@ -64,7 +64,11 @@ func (s *Service) GetResource(ctx context.Context, providerID string) ([]*Config
return nil, err
}

idps := s.idpConfiguration(cm.Data)
idps, err := s.idpConfiguration(cm.Data)

if err != nil {
return nil, err
}

if idps == nil {
return nil, nil
Expand All @@ -90,7 +94,11 @@ func (s *Service) EditResource(ctx context.Context, providerID string, data *Con
return nil, err
}

idps := s.idpConfiguration(cm.Data)
idps, err := s.idpConfiguration(cm.Data)

if err != nil {
return nil, err
}

if idps == nil {
return nil, nil
Expand Down Expand Up @@ -137,8 +145,11 @@ func (s *Service) CreateResource(ctx context.Context, data *Configuration) ([]*C
return nil, err
}

idps := s.idpConfiguration(cm.Data)
idps, err := s.idpConfiguration(cm.Data)

if err != nil {
return nil, err
}
if idps == nil {
return nil, nil
}
Expand Down Expand Up @@ -187,7 +198,11 @@ func (s *Service) DeleteResource(ctx context.Context, providerID string) error {
}

var found bool
idps := s.idpConfiguration(cm.Data)
idps, err := s.idpConfiguration(cm.Data)

if err != nil {
return err
}

if idps == nil {
return nil
Expand Down Expand Up @@ -292,25 +307,25 @@ func (s *Service) mergeConfiguration(base, update *Configuration) *Configuration
return base
}

func (s *Service) idpConfiguration(idps map[string]string) []*Configuration {
func (s *Service) idpConfiguration(idps map[string]string) ([]*Configuration, error) {

idpConfig := make([]*Configuration, 0)

rawIdps, ok := idps[s.keyName]

if !ok {
s.logger.Errorf("failed to find key %s in configMap %v", s.keyName, idps)
return nil
return idpConfig, nil
}

err := yaml.Unmarshal([]byte(rawIdps), &idpConfig)

if err != nil {
s.logger.Errorf("failed unmarshalling %s - %v", rawIdps, err)
return nil
return nil, fmt.Errorf("failed unmarshalling %s - %v", rawIdps, err)
}

return idpConfig
return idpConfig, nil
}

func (s *Service) keyIDMapper(id, namespace string) string {
Expand Down
4 changes: 2 additions & 2 deletions pkg/idp/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ func TestListResourcesFailsOnMissingKey(t *testing.T) {
mockLogger.EXPECT().Errorf(gomock.Any(), gomock.Any()).Times(1)
is, err := NewService(cfg, mockAuthz, mockTracer, mockMonitor, mockLogger).ListResources(ctx)

if is != nil {
t.Fatalf("expected result to be nil not %v", is)
if is != nil && len(is) > 0 {
t.Fatalf("expected result to be an empty slice not %v", is)

}

Expand Down

0 comments on commit 1cead53

Please sign in to comment.