-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
96 lines (79 loc) · 2.75 KB
/
manager.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
package license_manager
import (
"context"
"fmt"
"github.com/linuzilla/google-license-manager/config"
"github.com/linuzilla/google-license-manager/constants"
"github.com/linuzilla/google-license-manager/services/google_credential"
"google.golang.org/api/licensing/v1"
"google.golang.org/api/option"
"log"
"sync"
)
type LicenseManager interface {
AddLicenseToUser(userId string)
RevokeLicenseFromUser(userId string)
ListLicenseUser() []*licensing.LicenseAssignment
}
type licenseManagerImpl struct {
CredentialService google_credential.GoogleCredential `inject:"*"`
GoogleConfig *config.GoogleCfg `inject:"*"`
service *licensing.Service
newServiceOnce sync.Once
}
func New() LicenseManager {
return &licenseManagerImpl{}
}
func (impl *licenseManagerImpl) loadService() *licensing.Service {
impl.newServiceOnce.Do(func() {
ctx := context.Background()
credentialConfig := impl.CredentialService.LoadConfig()
credentialConfig.Client(ctx)
service, err := licensing.NewService(ctx,
option.WithHTTPClient(credentialConfig.Client(ctx)),
option.WithUserAgent(constants.UserAgent),
option.WithScopes(licensing.AppsLicensingScope))
if err != nil {
log.Fatal(err)
}
impl.service = service
})
return impl.service
}
func (impl *licenseManagerImpl) AddLicenseToUser(userId string) {
service := impl.loadService()
if assignment, err := service.LicenseAssignments.Insert(impl.GoogleConfig.ProductId, impl.GoogleConfig.ProductSkuId, &licensing.LicenseAssignmentInsert{
UserId: impl.CredentialService.NormalizeUserId(userId),
}).Do(); err != nil {
log.Print(err)
} else {
fmt.Printf("%s added\n", assignment.UserId)
}
}
func (impl *licenseManagerImpl) RevokeLicenseFromUser(userId string) {
service := impl.loadService()
user := impl.CredentialService.NormalizeUserId(userId)
if assignment, err := service.LicenseAssignments.Delete(impl.GoogleConfig.ProductId, impl.GoogleConfig.ProductSkuId, user).Do(); err != nil {
log.Print(err)
} else {
fmt.Printf("%s revoked (%d)\n", user, assignment.HTTPStatusCode)
}
}
func (impl *licenseManagerImpl) ListLicenseUser() []*licensing.LicenseAssignment {
service := impl.loadService()
var licenses []*licensing.LicenseAssignment
pageToken := ``
fields := service.LicenseAssignments.ListForProductAndSku(impl.GoogleConfig.ProductId, impl.GoogleConfig.ProductSkuId, impl.GoogleConfig.CustomerId).
Fields(`nextPageToken, items(productId, userId, skuId, skuName, productName)`)
for haveNextPage := true; haveNextPage; haveNextPage = pageToken != `` {
if r, err := fields.PageToken(pageToken).Do(); err != nil {
log.Print(err)
} else {
for _, item := range r.Items {
licenses = append(licenses, item)
}
pageToken = r.NextPageToken
}
}
return licenses
}