Skip to content

Commit

Permalink
use internal types for service principal methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vinay-gopalan committed Nov 10, 2023
1 parent 7a654b7 commit 359ed8f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
29 changes: 25 additions & 4 deletions api/service_principals.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ type ServicePrincipalClient interface {
DeleteServicePrincipal(ctx context.Context, spObjectID string, permanentlyDelete bool) error
}

type ServicePrincipal struct {
ID string
AppID string
}

func (c *MSGraphClient) CreateServicePrincipal(ctx context.Context, appID string, startDate time.Time, endDate time.Time) (string, string, error) {
spReq := models.NewServicePrincipal()
spReq.SetAppId(&appID)
Expand Down Expand Up @@ -60,7 +65,7 @@ func (c *MSGraphClient) DeleteServicePrincipal(ctx context.Context, spObjectID s
return err
}

func (c *MSGraphClient) ListServicePrincipals(ctx context.Context, spObjectID string) ([]models.ServicePrincipalable, error) {
func (c *MSGraphClient) ListServicePrincipals(ctx context.Context, spObjectID string) ([]ServicePrincipal, error) {
filter := fmt.Sprintf("appId eq '%s'", spObjectID)
requestParameters := &serviceprincipals.ServicePrincipalsRequestBuilderGetQueryParameters{
Filter: &filter,
Expand All @@ -78,9 +83,25 @@ func (c *MSGraphClient) ListServicePrincipals(ctx context.Context, spObjectID st
if err != nil {
return nil, err
}
return spList.GetValue(), nil

var result []ServicePrincipal
for _, sp := range spList.GetValue() {
result = append(result, ServicePrincipal{
ID: *sp.GetId(),
AppID: *sp.GetAppId(),
})
}
return result, nil
}

func (c *MSGraphClient) GetServicePrincipalByID(ctx context.Context, spObjectID string) (models.ServicePrincipalable, error) {
return c.client.ServicePrincipals().ByServicePrincipalId(spObjectID).Get(ctx, nil)
func (c *MSGraphClient) GetServicePrincipalByID(ctx context.Context, spObjectID string) (ServicePrincipal, error) {
sp, err := c.client.ServicePrincipals().ByServicePrincipalId(spObjectID).Get(ctx, nil)
if err != nil {
return ServicePrincipal{}, err
}

return ServicePrincipal{
ID: *sp.GetId(),
AppID: *sp.GetAppId(),
}, nil
}
8 changes: 4 additions & 4 deletions path_service_principal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1060,8 +1060,8 @@ func findServicePrincipalID(t *testing.T, client api.ServicePrincipalClient, app
}

for _, sp := range spList {
if *sp.GetAppId() == appID {
return *sp.GetId()
if sp.AppID == appID {
return sp.ID
}
}
default:
Expand All @@ -1081,11 +1081,11 @@ func assertServicePrincipalExists(t *testing.T, client api.ServicePrincipalClien
assertErrorIsNil(t, err)

if exists {
if *sp.GetId() == "" {
if sp.ID == "" {
t.Fatalf("Failed to find service principal")
}
} else {
if *sp.GetId() != "" {
if sp.ID != "" {
t.Fatalf("Found service principal when it shouldn't exist")
}
}
Expand Down

0 comments on commit 359ed8f

Please sign in to comment.