Skip to content

Commit

Permalink
add wrappers to perform nil checks on MSGraph responses
Browse files Browse the repository at this point in the history
  • Loading branch information
vinay-gopalan committed Nov 13, 2023
1 parent a653eb0 commit 5551bb2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 55 deletions.
84 changes: 39 additions & 45 deletions api/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,15 @@ type MSGraphClient struct {
}

type Application struct {
ID string
AppID string
AppObjectID string
Description string
DisplayName string
PasswordCredentials []PasswordCredential
}

type PasswordCredential struct {
DisplayName string
EndDate time.Time
KeyID string
SecretText string
EndDate time.Time
KeyID string
SecretText string
}

// NewMSGraphApplicationClient returns a new MSGraphClient configured to interact with
Expand Down Expand Up @@ -101,16 +97,7 @@ func (c *MSGraphClient) GetApplication(ctx context.Context, clientID string) (Ap

app := apps[0]

application := Application{
ID: *app.GetId(),
AppID: *app.GetAppId(),
AppObjectID: *app.GetId(),
Description: *app.GetDescription(),
DisplayName: *app.GetDisplayName(),
PasswordCredentials: getPasswordCredentialsForApplication(app),
}

return application, nil
return getApplicationResponse(app), nil
}

func (c *MSGraphClient) ListApplications(ctx context.Context, filter string) ([]Application, error) {
Expand All @@ -128,14 +115,7 @@ func (c *MSGraphClient) ListApplications(ctx context.Context, filter string) ([]

var apps []Application
for _, app := range resp.GetValue() {
apps = append(apps, Application{
ID: *app.GetId(),
AppID: *app.GetAppId(),
AppObjectID: *app.GetId(),
Description: *app.GetDescription(),
DisplayName: *app.GetDisplayName(),
PasswordCredentials: getPasswordCredentialsForApplication(app),
})
apps = append(apps, getApplicationResponse(app))
}

return apps, nil
Expand All @@ -151,14 +131,7 @@ func (c *MSGraphClient) CreateApplication(ctx context.Context, displayName strin
return Application{}, err
}

return Application{
ID: *resp.GetId(),
AppID: *resp.GetAppId(),
AppObjectID: *resp.GetId(),
Description: *resp.GetDescription(),
DisplayName: *resp.GetDisplayName(),
PasswordCredentials: getPasswordCredentialsForApplication(resp),
}, nil
return getApplicationResponse(resp), nil
}

// DeleteApplication deletes an Azure application object.
Expand Down Expand Up @@ -187,12 +160,7 @@ func (c *MSGraphClient) AddApplicationPassword(ctx context.Context, applicationO
return PasswordCredential{}, err
}

return PasswordCredential{
SecretText: *resp.GetSecretText(),
EndDate: *resp.GetEndDateTime(),
KeyID: resp.GetKeyId().String(),
DisplayName: *resp.GetDisplayName(),
}, nil
return getPasswordCredentialResponse(resp), nil
}

func (c *MSGraphClient) RemoveApplicationPassword(ctx context.Context, applicationObjectID string, keyID string) error {
Expand All @@ -210,12 +178,38 @@ func (c *MSGraphClient) RemoveApplicationPassword(ctx context.Context, applicati
func getPasswordCredentialsForApplication(app models.Applicationable) []PasswordCredential {
var appCredentials []PasswordCredential
for _, cred := range app.GetPasswordCredentials() {
appCredentials = append(appCredentials, PasswordCredential{
SecretText: *cred.GetSecretText(),
EndDate: *cred.GetEndDateTime(),
KeyID: cred.GetKeyId().String(),
DisplayName: *cred.GetDisplayName(),
})
appCredentials = append(appCredentials, getPasswordCredentialResponse(cred))
}
return appCredentials
}

func getApplicationResponse(app models.Applicationable) Application {
if app == nil {
return Application{
AppID: "",
AppObjectID: "",
PasswordCredentials: []PasswordCredential{},
}

}
return Application{
AppID: *app.GetAppId(),
AppObjectID: *app.GetId(),
PasswordCredentials: getPasswordCredentialsForApplication(app),
}
}

func getPasswordCredentialResponse(cred models.PasswordCredentialable) PasswordCredential {
if cred == nil {
return PasswordCredential{
SecretText: "",
EndDate: time.Time{},
KeyID: "",
}
}
return PasswordCredential{
SecretText: *cred.GetSecretText(),
EndDate: *cred.GetEndDateTime(),
KeyID: cred.GetKeyId().String(),
}
}
22 changes: 12 additions & 10 deletions api/service_principals.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/hashicorp/go-multierror"
abstractions "github.com/microsoft/kiota-abstractions-go"
"github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/microsoftgraph/msgraph-sdk-go/serviceprincipals"
)
Expand Down Expand Up @@ -71,11 +70,7 @@ func (c *MSGraphClient) ListServicePrincipals(ctx context.Context, spObjectID st
Filter: &filter,
}

headers := abstractions.NewRequestHeaders()
headers.Add("ConsistencyLevel", "eventual")

configuration := &serviceprincipals.ServicePrincipalsRequestBuilderGetRequestConfiguration{
Headers: headers,
QueryParameters: requestParameters,
}

Expand All @@ -86,10 +81,7 @@ func (c *MSGraphClient) ListServicePrincipals(ctx context.Context, spObjectID st

var result []ServicePrincipal
for _, sp := range spList.GetValue() {
result = append(result, ServicePrincipal{
ID: *sp.GetId(),
AppID: *sp.GetAppId(),
})
result = append(result, getServicePrincipalResponse(sp))
}
return result, nil
}
Expand All @@ -100,8 +92,18 @@ func (c *MSGraphClient) GetServicePrincipalByID(ctx context.Context, spObjectID
return ServicePrincipal{}, err
}

return getServicePrincipalResponse(sp), nil
}

func getServicePrincipalResponse(sp models.ServicePrincipalable) ServicePrincipal {
if sp == nil {
return ServicePrincipal{
ID: "",
AppID: "",
}
}
return ServicePrincipal{
ID: *sp.GetId(),
AppID: *sp.GetAppId(),
}, nil
}
}

0 comments on commit 5551bb2

Please sign in to comment.