Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions Device.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package msgraph

import (
"encoding/json"
"fmt"
"time"
)

// Device represents one device of ms graph
type Device struct {
ID string `json:"id,omitempty"`
DeletedDateTime time.Time `json:"deletedDateTime,omitempty"`
AccountEnabled bool `json:"accountEnabled,omitempty"`
ApproximateLastSignInDateTime time.Time `json:"approximateLastSignInDateTime,omitempty"`
ComplianceExpirationDateTime time.Time `json:"complianceExpirationDateTime,omitempty"`
DeviceId string `json:"deviceId,omitempty"`
DisplayName string `json:"displayName,omitempty"`
IsCompliant bool `json:"isCompliant,omitempty"`
IsManaged bool `json:"isManaged,omitempty"`
Manufacturer string `json:"manufacturer,omitempty"`
MdmAppId string `json:"mdmAppId,omitempty"`
Model string `json:"model,omitempty"`
OnPremisesLastSyncDateTime time.Time `json:"onPremisesLastSyncDateTime,omitempty"`
OnPremisesSyncEnabled bool `json:"onPremisesSyncEnabled,omitempty"`
OperatingSystem string `json:"operatingSystem,omitempty"`
OperatingSystemVersion string `json:"operatingSystemVersion,omitempty"`
PhysicalIds []string `json:"physicalIds,omitempty"`
ProfileType string `json:"profileType,omitempty"`
RegistrationDateTime time.Time `json:"registrationDateTime,omitempty"`
SystemLabels []string `json:"systemLabels,omitempty"`
TrustType string `json:"trustType,omitempty"`
MemberOf []Member `json:"memberOf,omitempty"`

graphClient *GraphClient // the graphClient that called the group
}

func (d Device) String() string {
return fmt.Sprintf("Device(ID: \"%v\", DeletedDateTime: \"%v\" AccountEnabled: \"%v\", ApproximateLastSignInDateTime: \"%v\", ComplianceExpirationDateTime: \"%v\", "+
"DeviceId: \"%v\", DisplayName: \"%v\", IsCompliant: \"%v\" IsManaged: \"%v\", Manufacturer: \"%v\", MdmAppId: \"%v\", Model: \"%v\", OnPremisesLastSyncDateTime: \"%v\", "+
"OnPremisesSyncEnabled: \"%v\", OperatingSystem: \"%v\", OperatingSystemVersion: \"%v\", PhysicalIds: \"%v\", ProfileType: \"%v\", RegistrationDateTime: \"%v\", "+
"SystemLabels: \"%v\", TrustType: \"%v\", DirectAPIConnection: %v)",
d.ID, d.DeletedDateTime, d.AccountEnabled, d.ApproximateLastSignInDateTime, d.ComplianceExpirationDateTime, d.DeviceId, d.DisplayName, d.IsCompliant,
d.IsManaged, d.Manufacturer, d.MdmAppId, d.Model, d.OnPremisesLastSyncDateTime, d.OnPremisesSyncEnabled, d.OperatingSystem,
d.OperatingSystemVersion, d.PhysicalIds, d.ProfileType, d.RegistrationDateTime, d.SystemLabels, d.TrustType, d.graphClient != nil)
}

// setGraphClient sets the graphClient instance in this instance and all child-instances (if any)
func (d *Device) setGraphClient(gC *GraphClient) {
d.graphClient = gC
}

// ListMembers - Get a list of the domain's direct members.
func (d Device) ListMembers(opts ...ListQueryOption) (Devices, error) {
if d.graphClient == nil {
return nil, ErrNotGraphClientSourced
}
resource := fmt.Sprintf("/devices/%v/members", d.ID)

var marsh struct {
Devices Devices `json:"value"`
}
marsh.Devices.setGraphClient(d.graphClient)
return marsh.Devices, d.graphClient.makeGETAPICall(resource, compileListQueryOptions(opts), &marsh)
}

// UnmarshalJSON implements the json unmarshal to be used by the json-library
func (d *Device) UnmarshalJSON(data []byte) error {
tmp := struct {
ID string `json:"id,omitempty"`
DeletedDateTime string `json:"deletedDateTime,omitempty"`
AccountEnabled bool `json:"accountEnabled,omitempty"`
ApproximateLastSignInDateTime string `json:"approximateLastSignInDateTime,omitempty"`
ComplianceExpirationDateTime string `json:"complianceExpirationDateTime,omitempty"`
DeviceId string `json:"deviceId,omitempty"`
DisplayName string `json:"displayName,omitempty"`
IsCompliant bool `json:"isCompliant,omitempty"`
IsManaged bool `json:"isManaged,omitempty"`
Manufacturer string `json:"manufacturer,omitempty"`
MdmAppId string `json:"mdmAppId,omitempty"`
Model string `json:"model,omitempty"`
OnPremisesLastSyncDateTime string `json:"onPremisesLastSyncDateTime,omitempty"`
OnPremisesSyncEnabled bool `json:"onPremisesSyncEnabled,omitempty"`
OperatingSystem string `json:"operatingSystem,omitempty"`
OperatingSystemVersion string `json:"operatingSystemVersion,omitempty"`
PhysicalIds []string `json:"physicalIds,omitempty"`
ProfileType string `json:"profileType,omitempty"`
RegistrationDateTime string `json:"registrationDateTime,omitempty"`
SystemLabels []string `json:"systemLabels,omitempty"`
TrustType string `json:"trustType,omitempty"`
MemberOf []Member `json:"memberOf,omitempty"`
}{}

err := json.Unmarshal(data, &tmp)
if err != nil {
return err
}

d.ID = tmp.ID
d.DeletedDateTime, err = time.Parse(time.RFC3339, tmp.DeletedDateTime)
if err != nil && tmp.DeletedDateTime != "" {
return fmt.Errorf("cannot parse DeletedDateTime %v with RFC3339: %v", tmp.DeletedDateTime, err)
}
d.AccountEnabled = tmp.AccountEnabled
d.ApproximateLastSignInDateTime, err = time.Parse(time.RFC3339, tmp.ApproximateLastSignInDateTime)
if err != nil && tmp.ApproximateLastSignInDateTime != "" {
return fmt.Errorf("cannot parse ApproximateLastSignInDateTime %v with RFC3339: %v", tmp.ApproximateLastSignInDateTime, err)
}
d.ComplianceExpirationDateTime, err = time.Parse(time.RFC3339, tmp.ComplianceExpirationDateTime)
if err != nil && tmp.ComplianceExpirationDateTime != "" {
return fmt.Errorf("cannot parse ApproximateLastSignInDateTime %v with RFC3339: %v", tmp.ComplianceExpirationDateTime, err)
}
d.DeviceId = tmp.DeviceId
d.OnPremisesSyncEnabled = tmp.OnPremisesSyncEnabled
d.DisplayName = tmp.DisplayName
d.IsCompliant = tmp.IsCompliant
d.IsManaged = tmp.IsManaged
d.Manufacturer = tmp.Manufacturer
d.MdmAppId = tmp.MdmAppId
d.Model = tmp.Model
d.OnPremisesLastSyncDateTime, err = time.Parse(time.RFC3339, tmp.OnPremisesLastSyncDateTime)
if err != nil && tmp.OnPremisesLastSyncDateTime != "" {
return fmt.Errorf("cannot parse OnPremisesLastSyncDateTime %v with RFC3339: %v", tmp.OnPremisesLastSyncDateTime, err)
}
d.OnPremisesSyncEnabled = tmp.OnPremisesSyncEnabled
d.OperatingSystem = tmp.OperatingSystem
d.OperatingSystemVersion = tmp.OperatingSystemVersion
d.PhysicalIds = tmp.PhysicalIds
d.ProfileType = tmp.ProfileType
d.RegistrationDateTime, err = time.Parse(time.RFC3339, tmp.RegistrationDateTime)
if err != nil && tmp.RegistrationDateTime != "" {
return fmt.Errorf("cannot parse OnPremisesLastSyncDateTime %v with RFC3339: %v", tmp.RegistrationDateTime, err)
}
d.SystemLabels = tmp.SystemLabels
d.TrustType = tmp.TrustType
d.MemberOf = tmp.MemberOf
return nil
}
49 changes: 49 additions & 0 deletions Device_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package msgraph

import (
"fmt"
"testing"
)

func GetTestDevice(t *testing.T) Device {
t.Helper()
devices, err := graphClient.ListDevices()
if err != nil {
t.Fatalf("Cannot GraphClient.ListDevice(): %v", err)
}
deviceTest, err := devices.GetByDisplayName(msGraphExistingDeviceDisplayName)
if err != nil {
t.Fatalf("Cannot devices.GetByDisplayName(%v): %v", msGraphExistingDeviceDisplayName, err)
}
return deviceTest
}

func TestDevice_String(t *testing.T) {
testDevice := GetTestDevice(t)

tests := []struct {
name string
g Device
want string
}{
{
name: "Test All Devices",
g: testDevice,
want: fmt.Sprintf("Device(ID: \"%v\", DeletedDateTime: \"%v\" AccountEnabled: \"%v\", ApproximateLastSignInDateTime: \"%v\", ComplianceExpirationDateTime: \"%v\", "+
"DeviceId: \"%v\", DisplayName: \"%v\", IsCompliant: \"%v\" IsManaged: \"%v\", Manufacturer: \"%v\", MdmAppId: \"%v\", Model: \"%v\", OnPremisesLastSyncDateTime: \"%v\", "+
"OnPremisesSyncEnabled: \"%v\", OperatingSystem: \"%v\", OperatingSystemVersion: \"%v\", PhysicalIds: \"%v\", ProfileType: \"%v\", RegistrationDateTime: \"%v\", "+
"SystemLabels: \"%v\", TrustType: \"%v\", DirectAPIConnection: %v)",
testDevice.ID, testDevice.DeletedDateTime, testDevice.AccountEnabled, testDevice.ApproximateLastSignInDateTime, testDevice.ComplianceExpirationDateTime, testDevice.DeviceId,
testDevice.DisplayName, testDevice.IsCompliant, testDevice.IsManaged, testDevice.Manufacturer, testDevice.MdmAppId, testDevice.Model, testDevice.OnPremisesLastSyncDateTime,
testDevice.OnPremisesSyncEnabled, testDevice.OperatingSystem, testDevice.OperatingSystemVersion, testDevice.PhysicalIds, testDevice.ProfileType, testDevice.RegistrationDateTime,
testDevice.SystemLabels, testDevice.TrustType, testDevice.graphClient != nil),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.g.String(); got != tt.want {
t.Errorf("Device.String() = %v, want %v", got, tt.want)
}
})
}
}
36 changes: 36 additions & 0 deletions Devices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package msgraph

import (
"strings"
)

// Devices represents multiple Device-instances and provides funcs to work with them.
type Devices []Device

func (d Devices) String() string {
var devices = make([]string, len(d))
for i, calendar := range d {
devices[i] = calendar.String()
}
return "Devices(" + strings.Join(devices, " | ") + ")"
}

// setGraphClient sets the GraphClient within that particular instance. Hence it's directly created by GraphClient
func (d Devices) setGraphClient(gC *GraphClient) Devices {
for i := range d {
d[i].setGraphClient(gC)
}
return d
}

// GetByDisplayName returns the Device obj of that array whose DisplayName matches
// the given name. Returns an ErrFindDevice if no device exists that matches the given
// DisplayName.
func (d Devices) GetByDisplayName(displayName string) (Device, error) {
for _, device := range d {
if device.DisplayName == displayName {
return device, nil
}
}
return Device{}, ErrFindDevice
}
48 changes: 48 additions & 0 deletions Domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package msgraph

import (
"fmt"
)

// Domain represents one domain of ms graph
type Domain struct {
ID string `json:"id,omitempty"`
AuthenticationType string `json:"authenticationType,omitempty"`
IsAdminManaged bool `json:"isAdminManaged,omitempty"`
IsDefault bool `json:"isDefault,omitempty"`
IsInitial bool `json:"isInitial,omitempty"`
IsRoot bool `json:"isRoot,omitempty"`
IsVerified bool `json:"isVerified,omitempty"`
SupportedServices []string `json:"supportedServices,omitempty"`
PasswordValidityPeriodInDays *int `json:"passwordValidityPeriodInDays,omitempty"`
PasswordNotificationWindowInDays *int `json:"passwordNotificationWindowInDays,omitempty"`
State interface{} `json:"state,omitempty"`

graphClient *GraphClient // the graphClient that called the group
}

func (d Domain) String() string {
return fmt.Sprintf("Domain(ID: \"%v\", AuthenticationType: \"%v\" IsAdminManaged: \"%v\", IsDefault: \"%v\", IsInitial: \"%v\", IsRoot: \"%v\", "+
"IsVerified: \"%v\", SupportedServices: \"%v\", PasswordValidityPeriodInDays: \"%v\", PasswordNotificationWindowInDays: \"%v\", DirectAPIConnection: %v)",
d.ID, d.AuthenticationType, d.IsAdminManaged, d.IsDefault, d.IsInitial, d.IsRoot, d.IsVerified, d.SupportedServices, d.PasswordValidityPeriodInDays,
d.PasswordNotificationWindowInDays, d.graphClient != nil)
}

// setGraphClient sets the graphClient instance in this instance and all child-instances (if any)
func (d *Domain) setGraphClient(gC *GraphClient) {
d.graphClient = gC
}

// ListMembers - Get a list of the domain's direct members.
func (d Domain) ListMembers(opts ...ListQueryOption) (Users, error) {
if d.graphClient == nil {
return nil, ErrNotGraphClientSourced
}
resource := fmt.Sprintf("/domains/%v/members", d.ID)

var marsh struct {
Users Users `json:"value"`
}
marsh.Users.setGraphClient(d.graphClient)
return marsh.Users, d.graphClient.makeGETAPICall(resource, compileListQueryOptions(opts), &marsh)
}
43 changes: 43 additions & 0 deletions Domain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package msgraph

import (
"fmt"
"testing"
)

func GetTestDomain(t *testing.T) Domain {
t.Helper()
domains, err := graphClient.ListDomains()
if err != nil {
t.Fatalf("Cannot GraphClient.ListDomains(): %v", err)
}
domainTest, err := domains.GetById(msGraphExistingDomainId)
if err != nil {
t.Fatalf("Cannot domains.GetById(%v): %v", msGraphExistingDomainId, err)
}
return domainTest
}

func TestDomain_String(t *testing.T) {
testDomain := GetTestDomain(t)

tests := []struct {
name string
g Domain
want string
}{
{
name: "Test All Domains",
g: testDomain,
want: fmt.Sprintf("Domain(ID: \"%v\", AuthenticationType: \"%v\" IsAdminManaged: \"%v\", IsDefault: \"%v\", IsInitial: \"%v\", IsRoot: \"%v\", IsVerified: \"%v\", SupportedServices: \"%v\", PasswordValidityPeriodInDays: \"%v\", PasswordNotificationWindowInDays: \"%v\", DirectAPIConnection: %v)",
testDomain.ID, testDomain.AuthenticationType, testDomain.IsAdminManaged, testDomain.IsDefault, testDomain.IsInitial, testDomain.IsRoot, testDomain.IsVerified, testDomain.SupportedServices, testDomain.PasswordValidityPeriodInDays, testDomain.PasswordNotificationWindowInDays, testDomain.graphClient != nil),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.g.String(); got != tt.want {
t.Errorf("Domain.String() = %v, want %v", got, tt.want)
}
})
}
}
36 changes: 36 additions & 0 deletions Domains.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package msgraph

import (
"strings"
)

// Domains represents multiple Domains-instances and provides funcs to work with them.
type Domains []Domain

func (d Domains) String() string {
var domains = make([]string, len(d))
for i, calendar := range d {
domains[i] = calendar.String()
}
return "Domains(" + strings.Join(domains, " | ") + ")"
}

// setGraphClient sets the GraphClient within that particular instance. Hence it's directly created by GraphClient
func (d Domains) setGraphClient(gC *GraphClient) Domains {
for i := range d {
d[i].setGraphClient(gC)
}
return d
}

// GetById returns the Domain obj of that array whose ID matches
// the given id. Returns an ErrFindDomain if no domain exists that matches the given
// DisplayName.
func (d Domains) GetById(id string) (Domain, error) {
for _, domain := range d {
if domain.ID == id {
return domain, nil
}
}
return Domain{}, ErrFindDomain
}
Loading