Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for generic directory objects and improved odata field handling #86

Merged
merged 10 commits into from
Aug 17, 2021
10 changes: 5 additions & 5 deletions msgraph/app_role_assignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
)

Expand Down Expand Up @@ -59,9 +59,9 @@ func (c *AppRoleAssignmentsClient) List(ctx context.Context, id string) (*[]AppR
return nil, status, fmt.Errorf("AppRoleAssignmentsClient.BaseClient.Get(): %v", err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var data struct {
AppRoleAssignments []AppRoleAssignment `json:"value"`
Expand Down Expand Up @@ -118,9 +118,9 @@ func (c *AppRoleAssignmentsClient) Assign(ctx context.Context, clientServicePrin
return nil, status, fmt.Errorf("AppRoleAssignmentsClient.BaseClient.Post(): %v", err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var appRoleAssignment AppRoleAssignment
if err := json.Unmarshal(respBody, &appRoleAssignment); err != nil {
Expand Down
55 changes: 25 additions & 30 deletions msgraph/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/manicminer/hamilton/odata"
Expand Down Expand Up @@ -38,9 +38,9 @@ func (c *ApplicationsClient) List(ctx context.Context, query odata.Query) (*[]Ap
return nil, status, fmt.Errorf("ApplicationsClient.BaseClient.Get(): %v", err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var data struct {
Applications []Application `json:"value"`
Expand Down Expand Up @@ -70,9 +70,9 @@ func (c *ApplicationsClient) Create(ctx context.Context, application Application
return nil, status, fmt.Errorf("ApplicationsClient.BaseClient.Post(): %v", err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var newApplication Application
if err := json.Unmarshal(respBody, &newApplication); err != nil {
Expand All @@ -96,9 +96,9 @@ func (c *ApplicationsClient) Get(ctx context.Context, id string, query odata.Que
return nil, status, fmt.Errorf("ApplicationsClient.BaseClient.Get(): %v", err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var application Application
if err := json.Unmarshal(respBody, &application); err != nil {
Expand All @@ -123,9 +123,9 @@ func (c *ApplicationsClient) GetDeleted(ctx context.Context, id string, query od
return nil, status, fmt.Errorf("ApplicationsClient.BaseClient.Get(): %v", err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var application Application
if err := json.Unmarshal(respBody, &application); err != nil {
Expand Down Expand Up @@ -207,7 +207,7 @@ func (c *ApplicationsClient) ListDeleted(ctx context.Context, query odata.Query)
return nil, status, err
}
defer resp.Body.Close()
respBody, _ := ioutil.ReadAll(resp.Body)
respBody, _ := io.ReadAll(resp.Body)
var data struct {
DeletedApps []Application `json:"value"`
}
Expand All @@ -232,9 +232,9 @@ func (c *ApplicationsClient) RestoreDeleted(ctx context.Context, id string) (*Ap
return nil, status, fmt.Errorf("ApplicationsClient.BaseClient.Post(): %v", err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var restoredApplication Application
if err = json.Unmarshal(respBody, &restoredApplication); err != nil {
Expand Down Expand Up @@ -267,9 +267,9 @@ func (c *ApplicationsClient) AddPassword(ctx context.Context, applicationId stri
return nil, status, fmt.Errorf("ApplicationsClient.BaseClient.Post(): %v", err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var newPasswordCredential PasswordCredential
if err := json.Unmarshal(respBody, &newPasswordCredential); err != nil {
Expand Down Expand Up @@ -320,9 +320,9 @@ func (c *ApplicationsClient) ListOwners(ctx context.Context, id string) (*[]stri
return nil, status, fmt.Errorf("ApplicationsClient.BaseClient.Get(): %v", err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var data struct {
Owners []struct {
Expand Down Expand Up @@ -357,9 +357,9 @@ func (c *ApplicationsClient) GetOwner(ctx context.Context, applicationId, ownerI
return nil, status, fmt.Errorf("ApplicationsClient.BaseClient.Get(): %v", err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var data struct {
Context string `json:"@odata.context"`
Expand All @@ -373,8 +373,8 @@ func (c *ApplicationsClient) GetOwner(ctx context.Context, applicationId, ownerI
return &data.Id, status, nil
}

// AddOwners adds a new owner to an Application.
// First populate the Owners field of the Application using the AppendOwner method of the model, then call this method.
// AddOwners adds new owners to an Application.
// First populate the `owners` field, then call this method
func (c *ApplicationsClient) AddOwners(ctx context.Context, application *Application) (int, error) {
var status int
if application.ID == nil {
Expand All @@ -392,12 +392,7 @@ func (c *ApplicationsClient) AddOwners(ctx context.Context, application *Applica
return false
}

data := struct {
Owner string `json:"@odata.id"`
}{
Owner: owner,
}
body, err := json.Marshal(data)
body, err := json.Marshal(DirectoryObject{ODataId: owner.ODataId})
if err != nil {
return status, fmt.Errorf("json.Marshal(): %v", err)
}
Expand Down Expand Up @@ -474,9 +469,9 @@ func (c *ApplicationsClient) ListExtensions(ctx context.Context, id string, quer
return nil, status, fmt.Errorf("ApplicationsClient.BaseClient.List(): %v", err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}

var data struct {
Expand Down Expand Up @@ -507,9 +502,9 @@ func (c *ApplicationsClient) CreateExtension(ctx context.Context, applicationExt
return nil, status, fmt.Errorf("ApplicationsClient.BaseClient.Post(): %v", err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}

var newApplicationExtension ApplicationExtension
Expand Down
41 changes: 39 additions & 2 deletions msgraph/applications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,52 @@ type ApplicationsClientTest struct {
}

func TestApplicationsClient(t *testing.T) {
rs := test.RandomString()
c := ApplicationsClientTest{
connection: test.NewConnection(auth.MsGraph, auth.TokenVersion2),
randomString: test.RandomString(),
randomString: rs,
}
c.client = msgraph.NewApplicationsClient(c.connection.AuthConfig.TenantID)
c.client.BaseClient.Authorizer = c.connection.Authorizer

token, err := c.connection.Authorizer.Token()
if err != nil {
t.Fatalf("could not acquire access token: %v", err)
}
claims, err := auth.ParseClaims(token)
if err != nil {
t.Fatalf("could not parse claims: %v", err)
}

u := UsersClientTest{
connection: test.NewConnection(auth.MsGraph, auth.TokenVersion2),
randomString: rs,
}
u.client = msgraph.NewUsersClient(u.connection.AuthConfig.TenantID)
u.client.BaseClient.Authorizer = u.connection.Authorizer

user := testUsersClient_Create(t, u, msgraph.User{
AccountEnabled: utils.BoolPtr(true),
DisplayName: utils.StringPtr("test-user-applicationowner"),
MailNickname: utils.StringPtr(fmt.Sprintf("test-user-applicationowner-%s", c.randomString)),
UserPrincipalName: utils.StringPtr(fmt.Sprintf("test-user-applicationowner-%s@%s", c.randomString, c.connection.DomainName)),
PasswordProfile: &msgraph.UserPasswordProfile{
Password: utils.StringPtr(fmt.Sprintf("IrPa55w0rd%s", c.randomString)),
},
})

o := DirectoryObjectsClientTest{
connection: test.NewConnection(auth.MsGraph, auth.TokenVersion2),
randomString: rs,
}
o.client = msgraph.NewDirectoryObjectsClient(c.connection.AuthConfig.TenantID)
o.client.BaseClient.Authorizer = o.connection.Authorizer

self := testDirectoryObjectsClient_Get(t, o, claims.ObjectId)

app := testApplicationsClient_Create(t, c, msgraph.Application{
DisplayName: utils.StringPtr(fmt.Sprintf("test-application-%s", c.randomString)),
Owners: &msgraph.Owners{*self},
})
testApplicationsClient_Get(t, c, *app.ID)
app.DisplayName = utils.StringPtr(fmt.Sprintf("test-app-updated-%s", c.randomString))
Expand All @@ -45,7 +82,7 @@ func TestApplicationsClient(t *testing.T) {
owners := testApplicationsClient_ListOwners(t, c, *app.ID)
testApplicationsClient_GetOwner(t, c, *app.ID, (*owners)[0])
testApplicationsClient_RemoveOwners(t, c, *app.ID, owners)
app.AppendOwner(c.client.BaseClient.Endpoint, c.client.BaseClient.ApiVersion, (*owners)[0])
app.Owners = &msgraph.Owners{*user.DirectoryObject}
testApplicationsClient_AddOwners(t, c, app)
pwd := testApplicationsClient_AddPassword(t, c, app)
testApplicationsClient_RemovePassword(t, c, app, pwd)
Expand Down
6 changes: 3 additions & 3 deletions msgraph/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type Client struct {

// HttpClient is the underlying http.Client, which by default uses a retryable client
HttpClient *http.Client
retryableClient *retryablehttp.Client
RetryableClient *retryablehttp.Client
}

// NewClient returns a new Client configured with the specified API version and tenant ID.
Expand All @@ -99,7 +99,7 @@ func NewClient(apiVersion ApiVersion, tenantId string) Client {
TenantId: tenantId,
UserAgent: "Hamilton (Go-http-client/1.1)",
HttpClient: r.StandardClient(),
retryableClient: r,
RetryableClient: r,
}
}

Expand Down Expand Up @@ -152,7 +152,7 @@ func (c Client) performRequest(req *http.Request, input HttpRequestInput) (*http
}
}

c.retryableClient.CheckRetry = func(ctx context.Context, resp *http.Response, err error) (bool, error) {
c.RetryableClient.CheckRetry = func(ctx context.Context, resp *http.Response, err error) (bool, error) {
if resp != nil && !c.DisableRetries {
if resp.StatusCode == http.StatusFailedDependency {
return true, nil
Expand Down
14 changes: 7 additions & 7 deletions msgraph/conditionalaccesspolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/manicminer/hamilton/odata"
Expand Down Expand Up @@ -38,9 +38,9 @@ func (c *ConditionalAccessPolicyClient) List(ctx context.Context, query odata.Qu
return nil, status, fmt.Errorf("ConditionalAccessPolicyClient.BaseClient.Get(): %v", err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var data struct {
ConditionalAccessPolicys []ConditionalAccessPolicy `json:"value"`
Expand Down Expand Up @@ -70,9 +70,9 @@ func (c *ConditionalAccessPolicyClient) Create(ctx context.Context, conditionalA
return nil, status, fmt.Errorf("ConditionalAccessPolicyClient.BaseClient.Post(): %v", err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var newConditionalAccessPolicy ConditionalAccessPolicy
if err := json.Unmarshal(respBody, &newConditionalAccessPolicy); err != nil {
Expand All @@ -96,9 +96,9 @@ func (c *ConditionalAccessPolicyClient) Get(ctx context.Context, id string, quer
return nil, status, fmt.Errorf("ConditionalAccessPolicyClient.BaseClient.Get(): %v", err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var conditionalAccessPolicy ConditionalAccessPolicy
if err := json.Unmarshal(respBody, &conditionalAccessPolicy); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions msgraph/directory_audit_reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/manicminer/hamilton/odata"
Expand Down Expand Up @@ -37,9 +37,9 @@ func (c *DirectoryAuditReportsClient) List(ctx context.Context, query odata.Quer
return nil, status, fmt.Errorf("DirectoryAuditReportsClient.BaseClient.Get(): %v", err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var data struct {
DirectoryAuditReports []DirectoryAudit `json:"value"`
Expand All @@ -65,9 +65,9 @@ func (c *DirectoryAuditReportsClient) Get(ctx context.Context, id string, query
return nil, status, fmt.Errorf("DirectoryAuditReportsClient.BaseClient.Get(): %v", err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var directoryAuditReport DirectoryAudit
if err := json.Unmarshal(respBody, &directoryAuditReport); err != nil {
Expand Down
Loading