forked from viamrobotics/rdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_client.go
2622 lines (2399 loc) · 75.9 KB
/
app_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package app contains the interfaces that manage a machine fleet with code instead of with the graphical interface of the Viam App.
//
// [fleet management docs]: https://docs.viam.com/appendix/apis/fleet/
package app
import (
"context"
"fmt"
"time"
packages "go.viam.com/api/app/packages/v1"
pb "go.viam.com/api/app/v1"
common "go.viam.com/api/common/v1"
"go.viam.com/utils/protoutils"
"go.viam.com/utils/rpc"
"google.golang.org/protobuf/types/known/timestamppb"
)
// Organization holds the information of an organization.
type Organization struct {
ID string
Name string
CreatedOn *time.Time
PublicNamespace string
DefaultRegion string
Cid *string
}
// OrganizationIdentity is used to render an organization's information on the frontend.
type OrganizationIdentity struct {
ID string
Name string
}
// OrgDetails holds the ID and name of the organization.
type OrgDetails struct {
OrgID string
OrgName string
}
// OrganizationMember holds the information of a member of an organization.
type OrganizationMember struct {
UserID string
Emails []string
DateAdded *time.Time
LastLogin *time.Time
}
// OrganizationInvite is the invite to an organization.
type OrganizationInvite struct {
OrganizationID string
Email string
CreatedOn *time.Time
Authorizations []*Authorization
}
// BillingAddress contains billing address details.
type BillingAddress struct {
AddressLine1 string
AddressLine2 *string
City string
State string
Zipcode string
}
// Location holds the information of a specific location.
type Location struct {
ID string
Name string
ParentLocationID string
Auth *LocationAuth
Organizations []*LocationOrganization
CreatedOn *time.Time
RobotCount int
Config *StorageConfig
}
// LocationOrganization holds information of an organization the location is shared with.
type LocationOrganization struct {
OrganizationID string
Primary bool
}
// StorageConfig holds the GCS region that data is stored in.
type StorageConfig struct {
Region string
}
// LocationAuth holds the secrets used to authenticate to a location.
type LocationAuth struct {
LocationID string
Secrets []*SharedSecret
}
// Robot holds the information of a machine.
type Robot struct {
ID string
Name string
Location string
LastAccess *time.Time
CreatedOn *time.Time
}
// RoverRentalRobot holds the information of a rover rental robot.
type RoverRentalRobot struct {
RobotID string
LocationID string
RobotName string
RobotMainPartID string
}
// RobotPart is a specific machine part.
type RobotPart struct {
ID string
Name string
DNSName string
Secret string
Robot string
LocationID string
RobotConfig map[string]interface{}
LastAccess *time.Time
UserSuppliedInfo map[string]interface{}
MainPart bool
FQDN string
LocalFQDN string
CreatedOn *time.Time
Secrets []*SharedSecret
LastUpdated *time.Time
}
// RobotPartHistoryEntry is a history entry of a robot part.
type RobotPartHistoryEntry struct {
Part string
Robot string
When *time.Time
Old *RobotPart
EditedBy *AuthenticatorInfo
}
// LogEntry holds the information of a single log entry.
type LogEntry struct {
Host string
Level string
Time *time.Time
LoggerName string
Message string
Caller map[string]interface{}
Stack string
Fields []map[string]interface{}
}
// Fragment stores the information of a fragment.
type Fragment struct {
ID string
Name string
Fragment map[string]interface{}
OrganizationOwner string
Public bool
CreatedOn *time.Time
OrganizationName string
RobotPartCount int
OrganizationCount int
OnlyUsedByOwner bool
Visibility FragmentVisibility
LastUpdated *time.Time
}
// FragmentVisibility specifies the kind of visibility a fragment has.
type FragmentVisibility int
const (
// FragmentVisibilityUnspecified is an unspecified visibility.
FragmentVisibilityUnspecified FragmentVisibility = iota
// FragmentVisibilityPrivate restricts access to a fragment to its organization.
FragmentVisibilityPrivate
// FragmentVisibilityPublic allows the fragment to be accessible to everyone.
FragmentVisibilityPublic
// FragmentVisibilityPublicUnlisted allows the fragment to be accessible to everyone but is hidden from public listings like it is private.
FragmentVisibilityPublicUnlisted
)
// FragmentHistoryEntry is an entry of a fragment's history.
type FragmentHistoryEntry struct {
Fragment string
EditedOn *time.Time
Old *Fragment
EditedBy *AuthenticatorInfo
}
// Authorization has the information about a specific authorization.
type Authorization struct {
AuthorizationType AuthRole
AuthorizationID string
ResourceType AuthResourceType
ResourceID string
IdentityID string
OrganizationID string
IdentityType string
}
// AuthorizedPermissions is authorized permissions.
type AuthorizedPermissions struct {
ResourceType string
ResourceID string
Permissions []string
}
// AuthRole represents the valid authorizaiton types for an Authorization.
type AuthRole string
const (
// AuthRoleOwner represents an owner authorization type.
AuthRoleOwner AuthRole = "owner"
// AuthRoleOperator represents an operator authorization type.
AuthRoleOperator AuthRole = "operator"
)
// AuthResourceType represents the valid authorization resource type for an Authorization.
type AuthResourceType string
const (
// AuthResourceTypeOrganization represents an organization authorization type.
AuthResourceTypeOrganization = "organization"
// AuthResourceTypeLocation represents a location authorization type.
AuthResourceTypeLocation = "location"
// AuthResourceTypeRobot represents a robot authorization type.
AuthResourceTypeRobot = "robot"
)
// APIKeyAuthorization is a struct with the necessary authorization data to create an API key.
type APIKeyAuthorization struct {
role AuthRole
resourceType AuthResourceType
resourceID string
}
// SharedSecret is a secret used for LocationAuth and RobotParts.
type SharedSecret struct {
ID string
CreatedOn *time.Time
State SharedSecretState
}
// SharedSecretState specifies if the secret is enabled, disabled, or unspecified.
type SharedSecretState int
const (
// SharedSecretStateUnspecified represents an unspecified shared secret state.
SharedSecretStateUnspecified SharedSecretState = iota
// SharedSecretStateEnabled represents an enabled secret that can be used in authentication.
SharedSecretStateEnabled
// SharedSecretStateDisabled represents a disabled secret that must not be used to authenticate to rpc.
SharedSecretStateDisabled
)
// AuthenticatorInfo holds the information of an authenticator.
type AuthenticatorInfo struct {
Type AuthenticationType
Value string
IsDeactivated bool
}
// AuthenticationType specifies the type of authentication.
type AuthenticationType int
const (
// AuthenticationTypeUnspecified represents an unspecified authentication.
AuthenticationTypeUnspecified AuthenticationType = iota
// AuthenticationTypeWebOAuth represents authentication using Web OAuth.
AuthenticationTypeWebOAuth
// AuthenticationTypeAPIKey represents authentication using an API key.
AuthenticationTypeAPIKey
// AuthenticationTypeRobotPartSecret represents authentication using a robot part secret.
AuthenticationTypeRobotPartSecret
// AuthenticationTypeLocationSecret represents authentication using a location secret.
AuthenticationTypeLocationSecret
)
// APIKeyWithAuthorizations is an API Key with its authorizations.
type APIKeyWithAuthorizations struct {
APIKey *APIKey
Authorizations []*AuthorizationDetails
}
// APIKey is a API key to make a request to an API.
type APIKey struct {
ID string
Key string
Name string
CreatedOn *time.Time
}
// AuthorizationDetails has the details for an authorization.
type AuthorizationDetails struct {
AuthorizationType string
AuthorizationID string
ResourceType string
ResourceID string
OrgID string
}
// RegistryItem has the information of an item in the registry.
type RegistryItem struct {
ItemID string
OrganizationID string
PublicNamespace string
Name string
Type PackageType
Visibility Visibility
URL string
Description string
TotalRobotUsage int
TotalExternalRobotUsage int
TotalOrganizationUsage int
TotalExternalOrganizationUsage int
Metadata isRegistryItemMetadata
CreatedAt *time.Time
UpdatedAt *time.Time
}
// RegistryItemStatus specifies if a registry item is published or in development.
type RegistryItemStatus int
const (
// RegistryItemStatusUnspecified is an unspecified registry item status.
RegistryItemStatusUnspecified RegistryItemStatus = iota
// RegistryItemStatusPublished represents a published registry item.
RegistryItemStatusPublished
// RegistryItemStatusInDevelopment represents a registry item still in development.
RegistryItemStatusInDevelopment
)
// PackageType is the type of package being used.
type PackageType int
const (
// PackageTypeUnspecified represents an unspecified package type.
PackageTypeUnspecified PackageType = iota
// PackageTypeArchive represents an archive package type.
PackageTypeArchive
// PackageTypeMLModel represents a ML model package type.
PackageTypeMLModel
// PackageTypeModule represents a module package type.
PackageTypeModule
// PackageTypeSLAMMap represents a SLAM map package type.
PackageTypeSLAMMap
// PackageTypeMLTraining represents a ML training package type.
PackageTypeMLTraining
)
// Visibility specifies the type of visibility of a registry item.
type Visibility int
const (
// VisibilityUnspecified represents an unspecified visibility.
VisibilityUnspecified Visibility = iota
// VisibilityPrivate are for registry items visible only within the owning org.
VisibilityPrivate
// VisibilityPublic are for registry items that are visible to everyone.
VisibilityPublic
// VisibilityPublicUnlisted are for registry items usable in everyone's robot but are hidden from the registry page as if they are private.
VisibilityPublicUnlisted
)
type isRegistryItemMetadata interface {
isRegistryItemMetadata()
}
type registryItemModuleMetadata struct {
ModuleMetadata *ModuleMetadata
}
type registryItemMLModelMetadata struct {
MlModelMetadata *MLModelMetadata
}
type registryItemMLTrainingMetadata struct {
MlTrainingMetadata *MLTrainingMetadata
}
// ModuleMetadata holds the metadata of a module.
type ModuleMetadata struct {
Models []*Model
Versions []*ModuleVersion
Entrypoint string
FirstRun *string
}
// Model holds the colon-delimited-triplet of the model and the API implemented by the model.
type Model struct {
API string
Model string
}
// ModuleVersion holds the information of a module version.
type ModuleVersion struct {
Version string
Files []*Uploads
Models []*Model
Entrypoint string
FirstRun *string
}
// Uploads holds the time the file was uploaded and the OS and architecture a module is built to run on.
type Uploads struct {
Platform string
UploadedAt *time.Time
}
// MLModelMetadata holds the metadata for a ML model.
type MLModelMetadata struct {
Versions []string
ModelType ModelType
ModelFramework ModelFramework
}
// MLTrainingMetadata is the metadata of an ML Training.
type MLTrainingMetadata struct {
Versions []*MLTrainingVersion
ModelType ModelType
ModelFramework ModelFramework
Draft bool
}
// MLTrainingVersion is the version of ML Training.
type MLTrainingVersion struct {
Version string
CreatedOn *time.Time
}
// Module holds the information of a module.
type Module struct {
ModuleID string
Name string
Visibility Visibility
Versions []*VersionHistory
URL string
Description string
Models []*Model
TotalRobotUsage int
TotalOrganizationUsage int
OrganizationID string
Entrypoint string
PublicNamespace string
FirstRun *string
}
// ModuleFileInfo holds the information of a module file.
type ModuleFileInfo struct {
ModuleID string
Version string
Platform string
PlatformTags []string
}
// VersionHistory holds the history of a version.
type VersionHistory struct {
Version string
Files []*Uploads
Models []*Model
Entrypoint string
FirstRun *string
}
// UpdateOrganizationOptions contains optional parameters for UpdateOrganization.
type UpdateOrganizationOptions struct {
Name *string
Namespace *string
// Region is the new GCS region to associate the org with.
Region *string
CID *string
}
// CreateOrganizationInviteOptions contains optional parameters for CreateOrganizationInvite.
type CreateOrganizationInviteOptions struct {
// SendEmailInvite defaults to true to send an email to the receipient of the invite.
// The user must accept the email to be added to the associated authorizations.
// If false, the user receives the associated authorization on the next login of the user with the associated email address.
SendEmailInvite *bool
}
// CreateLocationOptions contains optional parameters for CreateLocation.
type CreateLocationOptions struct {
// ParentLocationID is the parent location to move the location under.
ParentLocationID *string
}
// UpdateLocationOptions contains optional parameters for UpdateLocation.
type UpdateLocationOptions struct {
Name *string
// PArentLocationID is the new parent location to move the location under.
ParentLocationID *string
// Region is the GCS region to associate the location with.
Region *string
}
// GetRobotPartLogsOptions contains optional parameters for GetRobotPartLogs.
type GetRobotPartLogsOptions struct {
Filter *string
// PageToken represents the page to receive logs from. The function defaults to the most recent page if PageToken is empty.
PageToken *string
// Levels represents the levels of the logs requested. Logs of all levels are returned when levels is empty.
Levels []string
Start *time.Time
End *time.Time
Limit *int
Source *string
}
// TailRobotPartLogsOptions contains optional parameters for TailRobotPartLogs.
type TailRobotPartLogsOptions struct {
Filter *string
}
// CreateFragmentOptions contains optional parameters for CreateFragment.
type CreateFragmentOptions struct {
Visibility *FragmentVisibility
}
// UpdateFragmentOptions contains optional parameters for UpdateFragment.
type UpdateFragmentOptions struct {
Public *bool
Visibility *FragmentVisibility
}
// GetFragmentHistoryOptions contains optional parameters for GetFragmentHistory.
type GetFragmentHistoryOptions struct {
PageToken *string
PageLimit *int
}
// UpdateRegistryItemOptions contains optional parameters for UpdateRegistryItem.
type UpdateRegistryItemOptions struct {
URL *string
}
// ListRegistryItemsOptions contains optional parameters for ListRegistryItems.
type ListRegistryItemsOptions struct {
SearchTerm *string
PageToken *string
// PublicNamespaces are the namespaces to return results for.
PublicNamespaces []string
}
// UpdateModuleOptions contains optional parameters for UpdateModule.
type UpdateModuleOptions struct {
// The path to a setup script that is run before a newly downloaded module starts.
FirstRun *string
}
// ListModulesOptions contains optional parameters for ListModules.
type ListModulesOptions struct {
// OrgID is the organization to return private modules for.
OrgID *string
}
// AppClient is a gRPC client for method calls to the App API.
//
//nolint:revive // stutter: Ignore the "stuttering" warning for this type name
type AppClient struct {
client pb.AppServiceClient
}
func newAppClient(conn rpc.ClientConn) *AppClient {
return &AppClient{client: pb.NewAppServiceClient(conn)}
}
// GetUserIDByEmail gets the ID of the user with the given email.
func (c *AppClient) GetUserIDByEmail(ctx context.Context, email string) (string, error) {
resp, err := c.client.GetUserIDByEmail(ctx, &pb.GetUserIDByEmailRequest{
Email: email,
})
if err != nil {
return "", err
}
return resp.UserId, nil
}
// CreateOrganization creates a new organization.
func (c *AppClient) CreateOrganization(ctx context.Context, name string) (*Organization, error) {
resp, err := c.client.CreateOrganization(ctx, &pb.CreateOrganizationRequest{
Name: name,
})
if err != nil {
return nil, err
}
return organizationFromProto(resp.Organization), nil
}
// ListOrganizations lists all the organizations.
func (c *AppClient) ListOrganizations(ctx context.Context) ([]*Organization, error) {
resp, err := c.client.ListOrganizations(ctx, &pb.ListOrganizationsRequest{})
if err != nil {
return nil, err
}
var organizations []*Organization
for _, org := range resp.Organizations {
organizations = append(organizations, organizationFromProto(org))
}
return organizations, nil
}
// GetOrganizationsWithAccessToLocation gets all the organizations that have access to a location.
func (c *AppClient) GetOrganizationsWithAccessToLocation(ctx context.Context, locationID string) ([]*OrganizationIdentity, error) {
resp, err := c.client.GetOrganizationsWithAccessToLocation(ctx, &pb.GetOrganizationsWithAccessToLocationRequest{
LocationId: locationID,
})
if err != nil {
return nil, err
}
var organizations []*OrganizationIdentity
for _, org := range resp.OrganizationIdentities {
organizations = append(organizations, organizationIdentityFromProto(org))
}
return organizations, nil
}
// ListOrganizationsByUser lists all the organizations that a user belongs to.
func (c *AppClient) ListOrganizationsByUser(ctx context.Context, userID string) ([]*OrgDetails, error) {
resp, err := c.client.ListOrganizationsByUser(ctx, &pb.ListOrganizationsByUserRequest{
UserId: userID,
})
if err != nil {
return nil, err
}
var organizations []*OrgDetails
for _, org := range resp.Orgs {
organizations = append(organizations, orgDetailsFromProto(org))
}
return organizations, nil
}
// GetOrganization gets an organization.
func (c *AppClient) GetOrganization(ctx context.Context, orgID string) (*Organization, error) {
resp, err := c.client.GetOrganization(ctx, &pb.GetOrganizationRequest{
OrganizationId: orgID,
})
if err != nil {
return nil, err
}
return organizationFromProto(resp.Organization), nil
}
// GetOrganizationNamespaceAvailability checks for namespace availability throughout all organizations.
func (c *AppClient) GetOrganizationNamespaceAvailability(ctx context.Context, namespace string) (bool, error) {
resp, err := c.client.GetOrganizationNamespaceAvailability(ctx, &pb.GetOrganizationNamespaceAvailabilityRequest{
PublicNamespace: namespace,
})
if err != nil {
return false, err
}
return resp.Available, nil
}
// UpdateOrganization updates an organization.
func (c *AppClient) UpdateOrganization(ctx context.Context, orgID string, opts *UpdateOrganizationOptions) (*Organization, error) {
var name, namespace, region, cid *string
if opts != nil {
name, namespace, region, cid = opts.Name, opts.Namespace, opts.Region, opts.CID
}
resp, err := c.client.UpdateOrganization(ctx, &pb.UpdateOrganizationRequest{
OrganizationId: orgID,
Name: name,
PublicNamespace: namespace,
Region: region,
Cid: cid,
})
if err != nil {
return nil, err
}
return organizationFromProto(resp.Organization), nil
}
// DeleteOrganization deletes an organization.
func (c *AppClient) DeleteOrganization(ctx context.Context, orgID string) error {
_, err := c.client.DeleteOrganization(ctx, &pb.DeleteOrganizationRequest{
OrganizationId: orgID,
})
return err
}
// ListOrganizationMembers lists all members of an organization and all invited members to the organization.
func (c *AppClient) ListOrganizationMembers(ctx context.Context, orgID string) ([]*OrganizationMember, []*OrganizationInvite, error) {
resp, err := c.client.ListOrganizationMembers(ctx, &pb.ListOrganizationMembersRequest{
OrganizationId: orgID,
})
if err != nil {
return nil, nil, err
}
var members []*OrganizationMember
for _, member := range resp.Members {
members = append(members, organizationMemberFromProto(member))
}
var invites []*OrganizationInvite
for _, invite := range resp.Invites {
invites = append(invites, organizationInviteFromProto(invite))
}
return members, invites, nil
}
// CreateOrganizationInvite creates an organization invite to an organization.
func (c *AppClient) CreateOrganizationInvite(
ctx context.Context, orgID, email string, authorizations []*Authorization, opts *CreateOrganizationInviteOptions,
) (*OrganizationInvite, error) {
var pbAuthorizations []*pb.Authorization
for _, authorization := range authorizations {
pbAuthorizations = append(pbAuthorizations, authorizationToProto(authorization))
}
var send *bool
if opts != nil {
send = opts.SendEmailInvite
}
resp, err := c.client.CreateOrganizationInvite(ctx, &pb.CreateOrganizationInviteRequest{
OrganizationId: orgID,
Email: email,
Authorizations: pbAuthorizations,
SendEmailInvite: send,
})
if err != nil {
return nil, err
}
return organizationInviteFromProto(resp.Invite), nil
}
// UpdateOrganizationInviteAuthorizations updates the authorizations attached to an organization invite.
func (c *AppClient) UpdateOrganizationInviteAuthorizations(
ctx context.Context, orgID, email string, addAuthorizations, removeAuthorizations []*Authorization,
) (*OrganizationInvite, error) {
var pbAddAuthorizations []*pb.Authorization
for _, authorization := range addAuthorizations {
pbAddAuthorizations = append(pbAddAuthorizations, authorizationToProto(authorization))
}
var pbRemoveAuthorizations []*pb.Authorization
for _, authorization := range removeAuthorizations {
pbRemoveAuthorizations = append(pbRemoveAuthorizations, authorizationToProto(authorization))
}
resp, err := c.client.UpdateOrganizationInviteAuthorizations(ctx, &pb.UpdateOrganizationInviteAuthorizationsRequest{
OrganizationId: orgID,
Email: email,
AddAuthorizations: pbAddAuthorizations,
RemoveAuthorizations: pbRemoveAuthorizations,
})
if err != nil {
return nil, err
}
return organizationInviteFromProto(resp.Invite), nil
}
// DeleteOrganizationMember deletes an organization member from an organization.
func (c *AppClient) DeleteOrganizationMember(ctx context.Context, orgID, userID string) error {
_, err := c.client.DeleteOrganizationMember(ctx, &pb.DeleteOrganizationMemberRequest{
OrganizationId: orgID,
UserId: userID,
})
return err
}
// DeleteOrganizationInvite deletes an organization invite.
func (c *AppClient) DeleteOrganizationInvite(ctx context.Context, orgID, email string) error {
_, err := c.client.DeleteOrganizationInvite(ctx, &pb.DeleteOrganizationInviteRequest{
OrganizationId: orgID,
Email: email,
})
return err
}
// ResendOrganizationInvite resends an organization invite.
func (c *AppClient) ResendOrganizationInvite(ctx context.Context, orgID, email string) (*OrganizationInvite, error) {
resp, err := c.client.ResendOrganizationInvite(ctx, &pb.ResendOrganizationInviteRequest{
OrganizationId: orgID,
Email: email,
})
if err != nil {
return nil, err
}
return organizationInviteFromProto(resp.Invite), nil
}
// EnableBillingService enables a billing service to an address in an organization.
func (c *AppClient) EnableBillingService(ctx context.Context, orgID string, billingAddress *BillingAddress) error {
_, err := c.client.EnableBillingService(ctx, &pb.EnableBillingServiceRequest{
OrgId: orgID,
BillingAddress: billingAddressToProto(billingAddress),
})
return err
}
// DisableBillingService disables the billing service for an organization.
func (c *AppClient) DisableBillingService(ctx context.Context, orgID string) error {
_, err := c.client.DisableBillingService(ctx, &pb.DisableBillingServiceRequest{
OrgId: orgID,
})
return err
}
// UpdateBillingService updates the billing service of an organization.
func (c *AppClient) UpdateBillingService(ctx context.Context, orgID string, billingAddress *BillingAddress) error {
_, err := c.client.UpdateBillingService(ctx, &pb.UpdateBillingServiceRequest{
OrgId: orgID,
BillingAddress: billingAddressToProto(billingAddress),
})
return err
}
// OrganizationSetSupportEmail sets an organization's support email.
func (c *AppClient) OrganizationSetSupportEmail(ctx context.Context, orgID, email string) error {
_, err := c.client.OrganizationSetSupportEmail(ctx, &pb.OrganizationSetSupportEmailRequest{
OrgId: orgID,
Email: email,
})
return err
}
// OrganizationGetSupportEmail gets an organization's support email.
func (c *AppClient) OrganizationGetSupportEmail(ctx context.Context, orgID string) (string, error) {
resp, err := c.client.OrganizationGetSupportEmail(ctx, &pb.OrganizationGetSupportEmailRequest{
OrgId: orgID,
})
if err != nil {
return "", err
}
return resp.Email, nil
}
// GetBillingServiceConfig gets the billing service configuration for an organization.
func (c *AppClient) GetBillingServiceConfig(ctx context.Context, orgID string) (*pb.GetBillingServiceConfigResponse, error) {
resp, err := c.client.GetBillingServiceConfig(ctx, &pb.GetBillingServiceConfigRequest{
OrgId: orgID,
})
if err != nil {
return nil, err
}
return resp, nil
}
// OrganizationSetLogo sets an organization's logo.
func (c *AppClient) OrganizationSetLogo(ctx context.Context, orgID string, logo []byte) error {
_, err := c.client.OrganizationSetLogo(ctx, &pb.OrganizationSetLogoRequest{
OrgId: orgID,
Logo: logo,
})
return err
}
// OrganizationGetLogo gets an organization's logo.
func (c *AppClient) OrganizationGetLogo(ctx context.Context, orgID string) (string, error) {
resp, err := c.client.OrganizationGetLogo(ctx, &pb.OrganizationGetLogoRequest{
OrgId: orgID,
})
if err != nil {
return "", err
}
return resp.Url, nil
}
// ListOAuthApps gets the client's list of OAuth applications.
func (c *AppClient) ListOAuthApps(ctx context.Context, orgID string) ([]string, error) {
resp, err := c.client.ListOAuthApps(ctx, &pb.ListOAuthAppsRequest{
OrgId: orgID,
})
if err != nil {
return nil, err
}
return resp.ClientIds, nil
}
// CreateLocation creates a location with the given name under the given organization.
func (c *AppClient) CreateLocation(ctx context.Context, orgID, name string, opts *CreateLocationOptions) (*Location, error) {
var parentID *string
if opts != nil {
parentID = opts.ParentLocationID
}
resp, err := c.client.CreateLocation(ctx, &pb.CreateLocationRequest{
OrganizationId: orgID,
Name: name,
ParentLocationId: parentID,
})
if err != nil {
return nil, err
}
return locationFromProto(resp.Location), nil
}
// GetLocation gets a location.
func (c *AppClient) GetLocation(ctx context.Context, locationID string) (*Location, error) {
resp, err := c.client.GetLocation(ctx, &pb.GetLocationRequest{
LocationId: locationID,
})
if err != nil {
return nil, err
}
return locationFromProto(resp.Location), nil
}
// UpdateLocation updates a location.
func (c *AppClient) UpdateLocation(ctx context.Context, locationID string, opts *UpdateLocationOptions) (*Location, error) {
var name, parentID, region *string
if opts != nil {
name, parentID, region = opts.Name, opts.ParentLocationID, opts.Region
}
resp, err := c.client.UpdateLocation(ctx, &pb.UpdateLocationRequest{
LocationId: locationID,
Name: name,
ParentLocationId: parentID,
Region: region,
})
if err != nil {
return nil, err
}
return locationFromProto(resp.Location), nil
}
// DeleteLocation deletes a location.
func (c *AppClient) DeleteLocation(ctx context.Context, locationID string) error {
_, err := c.client.DeleteLocation(ctx, &pb.DeleteLocationRequest{
LocationId: locationID,
})
return err
}
// ListLocations gets a list of locations under the specified organization.
func (c *AppClient) ListLocations(ctx context.Context, orgID string) ([]*Location, error) {
resp, err := c.client.ListLocations(ctx, &pb.ListLocationsRequest{
OrganizationId: orgID,
})
if err != nil {
return nil, err
}
var locations []*Location
for _, location := range resp.Locations {
locations = append(locations, locationFromProto(location))
}
return locations, nil
}
// ShareLocation shares a location with an organization.
func (c *AppClient) ShareLocation(ctx context.Context, locationID, orgID string) error {
_, err := c.client.ShareLocation(ctx, &pb.ShareLocationRequest{
LocationId: locationID,
OrganizationId: orgID,
})
return err
}
// UnshareLocation stops sharing a location with an organization.
func (c *AppClient) UnshareLocation(ctx context.Context, locationID, orgID string) error {
_, err := c.client.UnshareLocation(ctx, &pb.UnshareLocationRequest{
LocationId: locationID,
OrganizationId: orgID,
})
return err
}
// LocationAuth gets a location's authorization secrets.
func (c *AppClient) LocationAuth(ctx context.Context, locationID string) (*LocationAuth, error) {
resp, err := c.client.LocationAuth(ctx, &pb.LocationAuthRequest{
LocationId: locationID,
})
if err != nil {
return nil, err
}
return locationAuthFromProto(resp.Auth), nil
}
// CreateLocationSecret creates a new generated secret in the location. Succeeds if there are no more than 2 active secrets after creation.
func (c *AppClient) CreateLocationSecret(ctx context.Context, locationID string) (*LocationAuth, error) {
resp, err := c.client.CreateLocationSecret(ctx, &pb.CreateLocationSecretRequest{
LocationId: locationID,
})
if err != nil {
return nil, err
}
return locationAuthFromProto(resp.Auth), nil
}
// DeleteLocationSecret deletes a secret from the location.
func (c *AppClient) DeleteLocationSecret(ctx context.Context, locationID, secretID string) error {
_, err := c.client.DeleteLocationSecret(ctx, &pb.DeleteLocationSecretRequest{
LocationId: locationID,
SecretId: secretID,
})
return err
}
// GetRobot gets a specific robot by ID.
func (c *AppClient) GetRobot(ctx context.Context, id string) (*Robot, error) {
resp, err := c.client.GetRobot(ctx, &pb.GetRobotRequest{
Id: id,
})
if err != nil {
return nil, err
}
return robotFromProto(resp.Robot), nil
}