Skip to content

Commit

Permalink
feat(api): implement crud operations for team members (#614)
Browse files Browse the repository at this point in the history
* feat(api): implement crud operations for team members
  • Loading branch information
vatasha committed Nov 30, 2021
1 parent a89177c commit 4aa40a2
Show file tree
Hide file tree
Showing 5 changed files with 764 additions and 1 deletion.
3 changes: 3 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ const (
apiV2ResourceGroupsFromGUID = "v2/ResourceGroups/%s"

apiV2Datasources = "v2/Datasources"

apiV2TeamMembers = "v2/TeamMembers"
apiV2TeamMembersFromGUID = "v2/TeamMembers/%s"
)

// WithApiV2 configures the client to use the API version 2 (/api/v2)
Expand Down
3 changes: 2 additions & 1 deletion api/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

package api

// SchemaService is the service that retrieves schemas for v2
// SchemasService is the service that retrieves schemas for v2
type SchemasService struct {
client *Client
Services map[integrationSchema]V2Service
Expand All @@ -33,6 +33,7 @@ const (
ContainerRegistries
CloudAccounts
ResourceGroups
TeamMembers
ReportRules
)

Expand Down
128 changes: 128 additions & 0 deletions api/team_members.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
//
// Author:: Vatasha White (<[email protected]>)
// Copyright:: Copyright 2021, Lacework Inc.
// License:: Apache License, Version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package api

import (
"errors"
"fmt"
)

type TeamMembersService struct {
client *Client
}

// NewTeamMember returns an instance of the Team Member struct
//
// Basic usage: Initialize a new TeamMember struct and then use the new instance to perform CRUD operations.
//
// client, err := api.NewClient("account")
// if err != nil {
// return err
// }
//
// teamMember := api.NewTeamMember(
// "FooBar",
// api.TeamMemberProps{
// Company: "ACME Inc",
// FirstName: "Foo",
// LastName: "Bar"
// },
// },
// )
//
// client.V2.TeamMembers.Create(teamMember)
//
func NewTeamMember(username string, props TeamMemberProps) TeamMember {
return TeamMember{
Props: props,
UserEnabled: 1,
UserName: username,
}
}

// List returns a list of team members
func (svc *TeamMembersService) List() (res TeamMembersResponse, err error) {
err = svc.client.RequestDecoder("GET", apiV2TeamMembers, nil, &res)
return
}

// Create creates a single team member
func (svc *TeamMembersService) Create(tm TeamMember) (res TeamMemberResponse, err error) {
err = svc.client.RequestEncoderDecoder("POST", apiV2TeamMembers, tm, &res)
return
}

// Delete deletes a single team member with the corresponding guid
func (svc *TeamMembersService) Delete(guid string) error {
if guid == "" {
return errors.New("please specify a guid")
}

return svc.client.RequestDecoder("DELETE", fmt.Sprintf(apiV2TeamMembersFromGUID, guid), nil, nil)
}

// Update updates a single team member with the corresponding guid
func (svc *TeamMembersService) Update(tm TeamMember) (res TeamMemberResponse, err error) {
if tm.UserGuid == "" {
err = errors.New("please specify a guid")
return
}
err = svc.client.RequestEncoderDecoder("PATCH", fmt.Sprintf(apiV2TeamMembersFromGUID, tm.UserGuid), tm, &res)
return
}

// Get returns a response of the team member
func (svc *TeamMembersService) Get(guid string, res interface{}) error {
if guid == "" {
return errors.New("please specify a guid")
}
return svc.client.RequestDecoder("GET", fmt.Sprintf(apiV2TeamMembersFromGUID, guid), nil, &res)

}

type TeamMemberProps struct {
AccountAdmin bool `json:"accountAdmin,omitempty"`
Company string `json:"company"`
CreatedTime string `json:"createdTime,omitempty"`
FirstName string `json:"firstName"`
JitCreated bool `json:"jitCreated,omitempty"`
LastLoginTime interface{} `json:"lastLoginTime,omitempty"`
LastName string `json:"lastName"`
LastSessionCreatedTime interface{} `json:"lastSessionCreatedTime,omitempty"`
OrgAdmin bool `json:"orgAdmin,omitempty"`
OrgUser bool `json:"orgUser,omitempty"`
UpdatedBy string `json:"updatedBy,omitempty"`
UpdatedTime interface{} `json:"updatedTime,omitempty"`
}

type TeamMember struct {
CustGuid string `json:"custGuid,omitempty"`
Props TeamMemberProps `json:"props"`
UserEnabled int `json:"userEnabled"`
UserGuid string `json:"userGuid,omitempty"`
UserName string `json:"userName"`
}

type TeamMemberResponse struct {
Data TeamMember `json:"data"`
}

type TeamMembersResponse struct {
Data []TeamMember `json:"data"`
}
Loading

0 comments on commit 4aa40a2

Please sign in to comment.