Skip to content

Commit

Permalink
Add MDM recovery password configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
np5 committed Aug 6, 2023
1 parent 5cfcd8b commit 436d2ba
Show file tree
Hide file tree
Showing 5 changed files with 515 additions and 45 deletions.
14 changes: 8 additions & 6 deletions goztl.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

const (
libraryVersion = "0.1.36"
libraryVersion = "0.1.37"
userAgent = "goztl/" + libraryVersion
mediaType = "application/json"
)
Expand All @@ -38,11 +38,12 @@ type Client struct {
Tags TagsService
Taxonomies TaxonomiesService
// MDM
MDMArtifacts MDMArtifactsService
MDMBlueprints MDMBlueprintsService
MDMBlueprintArtifacts MDMBlueprintArtifactsService
MDMFileVaultConfigs MDMFileVaultConfigsService
MDMProfiles MDMProfilesService
MDMArtifacts MDMArtifactsService
MDMBlueprints MDMBlueprintsService
MDMBlueprintArtifacts MDMBlueprintArtifactsService
MDMFileVaultConfigs MDMFileVaultConfigsService
MDMProfiles MDMProfilesService
MDMRecoveryPasswordConfigs MDMRecoveryPasswordConfigsService
// Monolith
MonolithCatalogs MonolithCatalogsService
MonolithConditions MonolithConditionsService
Expand Down Expand Up @@ -160,6 +161,7 @@ func NewClient(httpClient *http.Client, bu string, token string, opts ...ClientO
c.MDMBlueprintArtifacts = &MDMBlueprintArtifactsServiceOp{client: c}
c.MDMFileVaultConfigs = &MDMFileVaultConfigsServiceOp{client: c}
c.MDMProfiles = &MDMProfilesServiceOp{client: c}
c.MDMRecoveryPasswordConfigs = &MDMRecoveryPasswordConfigsServiceOp{client: c}
// Monolith
c.MonolithCatalogs = &MonolithCatalogsServiceOp{client: c}
c.MonolithConditions = &MonolithConditionsServiceOp{client: c}
Expand Down
32 changes: 17 additions & 15 deletions mdm_blueprints.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ var _ MDMBlueprintsService = &MDMBlueprintsServiceOp{}

// MDMBlueprint represents a Zentral MDM blueprint
type MDMBlueprint struct {
ID int `json:"id,omitempty"`
Name string `json:"name"`
InventoryInterval int `json:"inventory_interval"`
CollectApps int `json:"collect_apps"`
CollectCertificates int `json:"collect_certificates"`
CollectProfiles int `json:"collect_profiles"`
FileVaultConfigID *int `json:"filevault_config"`
Created Timestamp `json:"created_at,omitempty"`
Updated Timestamp `json:"updated_at,omitempty"`
ID int `json:"id,omitempty"`
Name string `json:"name"`
InventoryInterval int `json:"inventory_interval"`
CollectApps int `json:"collect_apps"`
CollectCertificates int `json:"collect_certificates"`
CollectProfiles int `json:"collect_profiles"`
FileVaultConfigID *int `json:"filevault_config"`
RecoveryPasswordConfigID *int `json:"recovery_password_config"`
Created Timestamp `json:"created_at,omitempty"`
Updated Timestamp `json:"updated_at,omitempty"`
}

func (mb MDMBlueprint) String() string {
Expand All @@ -46,12 +47,13 @@ func (mb MDMBlueprint) String() string {

// MDMBlueprintRequest represents a request to create or update a MDM blueprint
type MDMBlueprintRequest struct {
Name string `json:"name"`
InventoryInterval int `json:"inventory_interval"`
CollectApps int `json:"collect_apps"`
CollectCertificates int `json:"collect_certificates"`
CollectProfiles int `json:"collect_profiles"`
FileVaultConfigID *int `json:"filevault_config"`
Name string `json:"name"`
InventoryInterval int `json:"inventory_interval"`
CollectApps int `json:"collect_apps"`
CollectCertificates int `json:"collect_certificates"`
CollectProfiles int `json:"collect_profiles"`
FileVaultConfigID *int `json:"filevault_config"`
RecoveryPasswordConfigID *int `json:"recovery_password_config"`
}

type listMBOptions struct {
Expand Down
54 changes: 30 additions & 24 deletions mdm_blueprints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var mbListJSONResponse = `
"collect_certificates": 1,
"collect_profiles": 2,
"filevault_config": null,
"recovery_password_config": null,
"created_at": "2022-07-22T01:02:03.444444",
"updated_at": "2022-07-22T01:02:03.444444"
}
Expand All @@ -36,6 +37,7 @@ var mbGetJSONResponse = `
"collect_certificates": 1,
"collect_profiles": 2,
"filevault_config": 3,
"recovery_password_config": 4,
"created_at": "2022-07-22T01:02:03.444444",
"updated_at": "2022-07-22T01:02:03.444444"
}
Expand Down Expand Up @@ -63,6 +65,7 @@ var mbUpdateJSONResponse = `
"collect_certificates": 1,
"collect_profiles": 2,
"filevault_config": 3,
"recovery_password_config": 4,
"created_at": "2022-07-22T01:02:03.444444",
"updated_at": "2022-07-22T01:02:03.444444"
}
Expand Down Expand Up @@ -118,15 +121,16 @@ func TestMDMBlueprintsService_GetByID(t *testing.T) {
}

want := &MDMBlueprint{
ID: 4,
Name: "Default",
InventoryInterval: 77777,
CollectApps: 0,
CollectCertificates: 1,
CollectProfiles: 2,
FileVaultConfigID: Int(3),
Created: Timestamp{referenceTime},
Updated: Timestamp{referenceTime},
ID: 4,
Name: "Default",
InventoryInterval: 77777,
CollectApps: 0,
CollectCertificates: 1,
CollectProfiles: 2,
FileVaultConfigID: Int(3),
RecoveryPasswordConfigID: Int(4),
Created: Timestamp{referenceTime},
Updated: Timestamp{referenceTime},
}
if !cmp.Equal(got, want) {
t.Errorf("MDMBlueprints.GetByID returned %+v, want %+v", got, want)
Expand Down Expand Up @@ -217,12 +221,13 @@ func TestMDMBlueprintsService_Update(t *testing.T) {
defer teardown()

updateRequest := &MDMBlueprintRequest{
Name: "Default",
InventoryInterval: 77777,
CollectApps: 0,
CollectCertificates: 1,
CollectProfiles: 2,
FileVaultConfigID: Int(3),
Name: "Default",
InventoryInterval: 77777,
CollectApps: 0,
CollectCertificates: 1,
CollectProfiles: 2,
FileVaultConfigID: Int(3),
RecoveryPasswordConfigID: Int(4),
}

mux.HandleFunc("/mdm/blueprints/4/", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -245,15 +250,16 @@ func TestMDMBlueprintsService_Update(t *testing.T) {
}

want := &MDMBlueprint{
ID: 4,
Name: "Default",
InventoryInterval: 77777,
CollectApps: 0,
CollectCertificates: 1,
CollectProfiles: 2,
FileVaultConfigID: Int(3),
Created: Timestamp{referenceTime},
Updated: Timestamp{referenceTime},
ID: 4,
Name: "Default",
InventoryInterval: 77777,
CollectApps: 0,
CollectCertificates: 1,
CollectProfiles: 2,
FileVaultConfigID: Int(3),
RecoveryPasswordConfigID: Int(4),
Created: Timestamp{referenceTime},
Updated: Timestamp{referenceTime},
}
if !cmp.Equal(got, want) {
t.Errorf("MDMBlueprints.Update returned %+v, want %+v", got, want)
Expand Down
194 changes: 194 additions & 0 deletions mdm_recovery_password_configs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package goztl

import (
"context"
"fmt"
"net/http"
)

const mrpcBasePath = "mdm/recovery_password_configs/"

// MDMRecoveryPasswordConfigsService is an interface for interfacing with the MDM recovery password configuration
// endpoints of the Zentral API
type MDMRecoveryPasswordConfigsService interface {
List(context.Context, *ListOptions) ([]MDMRecoveryPasswordConfig, *Response, error)
GetByID(context.Context, int) (*MDMRecoveryPasswordConfig, *Response, error)
GetByName(context.Context, string) (*MDMRecoveryPasswordConfig, *Response, error)
Create(context.Context, *MDMRecoveryPasswordConfigRequest) (*MDMRecoveryPasswordConfig, *Response, error)
Update(context.Context, int, *MDMRecoveryPasswordConfigRequest) (*MDMRecoveryPasswordConfig, *Response, error)
Delete(context.Context, int) (*Response, error)
}

// MDMRecoveryPasswordConfigsServiceOp handles communication with the MDM recovery password configurations related
// methods of the Zentral API.
type MDMRecoveryPasswordConfigsServiceOp struct {
client *Client
}

var _ MDMRecoveryPasswordConfigsService = &MDMRecoveryPasswordConfigsServiceOp{}

// MDMRecoveryPasswordConfig represents a Zentral MDM recovery password configuration
type MDMRecoveryPasswordConfig struct {
ID int `json:"id"`
Name string `json:"name"`
DynamicPassword bool `json:"dynamic_password"`
StaticPassword *string `json:"static_password"`
RotationIntervalDays int `json:"rotation_interval_days"`
RotateFirmwarePassword bool `json:"rotate_firmware_password"`
Created Timestamp `json:"created_at,omitempty"`
Updated Timestamp `json:"updated_at,omitempty"`
}

func (mrpc MDMRecoveryPasswordConfig) String() string {
return Stringify(mrpc)
}

// MDMRecoveryPasswordConfigRequest represents a request to create or update a MDM recovery password configuration
type MDMRecoveryPasswordConfigRequest struct {
Name string `json:"name"`
DynamicPassword bool `json:"dynamic_password"`
StaticPassword *string `json:"static_password"`
RotationIntervalDays int `json:"rotation_interval_days"`
RotateFirmwarePassword bool `json:"rotate_firmware_password"`
}

type listMRPCOptions struct {
Name string `url:"name,omitempty"`
}

// List lists all the MDM recovery password configurations.
func (s *MDMRecoveryPasswordConfigsServiceOp) List(ctx context.Context, opt *ListOptions) ([]MDMRecoveryPasswordConfig, *Response, error) {
return s.list(ctx, opt, nil)
}

// GetByID retrieves a MDM recovery password configuration by id.
func (s *MDMRecoveryPasswordConfigsServiceOp) GetByID(ctx context.Context, mrpcID int) (*MDMRecoveryPasswordConfig, *Response, error) {
if mrpcID < 1 {
return nil, nil, NewArgError("mrpcID", "cannot be less than 1")
}

path := fmt.Sprintf("%s%d/", mrpcBasePath, mrpcID)

req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

mrpc := new(MDMRecoveryPasswordConfig)

resp, err := s.client.Do(ctx, req, mrpc)
if err != nil {
return nil, resp, err
}

return mrpc, resp, err
}

// GetByName retrieves a MDM recovery password configuration by name.
func (s *MDMRecoveryPasswordConfigsServiceOp) GetByName(ctx context.Context, name string) (*MDMRecoveryPasswordConfig, *Response, error) {
if len(name) < 1 {
return nil, nil, NewArgError("name", "cannot be blank")
}

listMRPCOpt := &listMRPCOptions{Name: name}

mrpcs, resp, err := s.list(ctx, nil, listMRPCOpt)
if err != nil {
return nil, resp, err
}
if len(mrpcs) < 1 {
return nil, resp, nil
}

return &mrpcs[0], resp, err
}

// Create a new MDM recovery password configuration.
func (s *MDMRecoveryPasswordConfigsServiceOp) Create(ctx context.Context, createRequest *MDMRecoveryPasswordConfigRequest) (*MDMRecoveryPasswordConfig, *Response, error) {
if createRequest == nil {
return nil, nil, NewArgError("createRequest", "cannot be nil")
}

req, err := s.client.NewRequest(ctx, http.MethodPost, mrpcBasePath, createRequest)
if err != nil {
return nil, nil, err
}

mrpc := new(MDMRecoveryPasswordConfig)
resp, err := s.client.Do(ctx, req, mrpc)
if err != nil {
return nil, resp, err
}

return mrpc, resp, err
}

// Update a MDM recovery password configuration.
func (s *MDMRecoveryPasswordConfigsServiceOp) Update(ctx context.Context, mrpcID int, updateRequest *MDMRecoveryPasswordConfigRequest) (*MDMRecoveryPasswordConfig, *Response, error) {
if mrpcID < 1 {
return nil, nil, NewArgError("mrpcID", "cannot be less than 1")
}

if updateRequest == nil {
return nil, nil, NewArgError("updateRequest", "cannot be nil")
}

path := fmt.Sprintf("%s%d/", mrpcBasePath, mrpcID)

req, err := s.client.NewRequest(ctx, http.MethodPut, path, updateRequest)
if err != nil {
return nil, nil, err
}

mrpc := new(MDMRecoveryPasswordConfig)
resp, err := s.client.Do(ctx, req, mrpc)
if err != nil {
return nil, resp, err
}

return mrpc, resp, err
}

// Delete a MDM recovery password configuration.
func (s *MDMRecoveryPasswordConfigsServiceOp) Delete(ctx context.Context, mrpcID int) (*Response, error) {
if mrpcID < 1 {
return nil, NewArgError("mrpcID", "cannot be less than 1")
}

path := fmt.Sprintf("%s%d/", mrpcBasePath, mrpcID)

req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}

resp, err := s.client.Do(ctx, req, nil)

return resp, err
}

// Helper method for listing MDM recovery password configurations
func (s *MDMRecoveryPasswordConfigsServiceOp) list(ctx context.Context, opt *ListOptions, mrpcOpt *listMRPCOptions) ([]MDMRecoveryPasswordConfig, *Response, error) {
path := mrpcBasePath
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
path, err = addOptions(path, mrpcOpt)
if err != nil {
return nil, nil, err
}

req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

var mrpcs []MDMRecoveryPasswordConfig
resp, err := s.client.Do(ctx, req, &mrpcs)
if err != nil {
return nil, resp, err
}

return mrpcs, resp, err
}
Loading

0 comments on commit 436d2ba

Please sign in to comment.