-
Notifications
You must be signed in to change notification settings - Fork 2
/
appinstance.go
155 lines (134 loc) · 4.51 KB
/
appinstance.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
package cloud
import (
"errors"
"fmt"
"github.com/rs/xid"
)
type appInstanceRequest struct {
OTP string `json:"otp"`
ParentAppID string `json:"parent_application_id"`
InstanceID string `json:"instance_identifier"`
InstanceName string `json:"instance_name"`
}
type appInstanceResponse struct {
AppID string `json:"app_id"`
AppApiKey string `json:"app_api_key"`
TenantToken string `json:"api_token"`
TenantID string `json:"tenant_id"`
TenantName string `json:"tenant_name"`
}
type createAppInstanceRequest struct {
InstanceID string `json:"instance_identifier"`
InstanceName string `json:"instance_name"`
}
type createAppInstanceResponse struct {
ID string `json:"application_id"`
ApiKey string `json:"api_key"`
}
// LinkTenantWithNewAppInstance redeems the OTP and links a tenant to a new application instance.
// InstanceName will be shown in UI to signify the new application instance.
// The returned App.ID(), App.ApiKey(), Tenant.ID(), Tenant.Name() and Tenant.ApiToken() must be stored securely.
//
// Deprecated: Use CreateAppInstance and LinkTenant instead.
func (app *App) LinkTenantWithNewAppInstance(otp, instanceName string) (*App, *Tenant, error) {
appReq := appInstanceRequest{
OTP: otp,
ParentAppID: app.config.ID,
InstanceID: xid.New().String(),
InstanceName: instanceName,
}
var appResp appInstanceResponse
var errorResp errorResponse
r, err := app.httpClient.R().
SetBody(appReq).
SetResult(&appResp).
SetError(&errorResp).
Post(newAppInstancePath)
if err != nil {
return nil, nil, err
}
if r.IsError() {
return nil, nil, errors.New(errorResp.GetError())
}
appConfig := app.newAppConfig(appResp.AppID, appResp.AppApiKey)
childApp, err := New(appConfig)
if err != nil {
return nil, nil, err
}
tenant, err := childApp.SetTenant(appResp.TenantID, appResp.TenantName, appResp.TenantToken)
if err != nil {
return nil, nil, err
}
return childApp, tenant, nil
}
// CreateAppInstance creates an app instance. The instance can then be used to LinkTenant.
// This is called with the parent app, using the parent app_key to authenticate
// InstanceName will be shown in UI to signify the new application instance.
// The returned App.ID() and App.ApiKey() must be stored securely.
// This can only be used with application that is registered as multi-instance.
func (app *App) CreateAppInstance(instanceName string) (*App, error) {
req := createAppInstanceRequest{
InstanceID: xid.New().String(),
InstanceName: instanceName,
}
var resp createAppInstanceResponse
var errorResp errorResponse
path := fmt.Sprintf(createAppInstancePath, app.config.ID)
r, err := app.httpClient.R().
SetBody(req).
SetResult(&resp).
SetError(&errorResp).
Post(path)
if err != nil {
return nil, err
}
if r.IsError() {
return nil, errors.New(errorResp.GetError())
}
appConfig := app.newAppConfig(resp.ID, resp.ApiKey)
return New(appConfig)
}
// DeleteAppInstance deletes an app instance using the AppId of the instance
// This is called with the parent app, using the parent app_key to authenticate.
func (app *App) DeleteAppInstance(instanceAppId string) error {
var errorResp errorResponse
path := fmt.Sprintf(deleteAppInstancePath, instanceAppId)
r, err := app.httpClient.R().
SetError(&errorResp).
Delete(path)
if err != nil {
return err
}
if r.IsError() {
return errors.New(errorResp.GetError())
}
return nil
}
// SetAppInstance adds an application instance.
// The appID and appApiKey are obtained from calling LinkTenantWithNewAppInstance.
// This should be used by application after restart to reload application instances
func (app *App) SetAppInstance(appID, appApiKey string) (*App, error) {
config := app.newAppConfig(appID, appApiKey)
return New(config)
}
func (app *App) newAppConfig(appID, appApiKey string) Config {
return Config{
ID: appID,
GlobalFQDN: app.config.GlobalFQDN,
RegionalFQDN: app.config.RegionalFQDN,
ReadStreamID: "app--" + appID + "-R",
WriteStreamID: "app--" + appID + "-W",
Transport: app.config.Transport,
ApiKey: appApiKey,
DeviceActivationHandler: app.config.DeviceActivationHandler,
DeviceDeactivationHandler: app.config.DeviceDeactivationHandler,
TenantUnlinkedHandler: app.config.TenantUnlinkedHandler,
DeviceMessageHandler: app.config.DeviceMessageHandler,
}
}
func (app *App) ID() string {
return app.config.ID
}
func (app *App) ApiKey() string {
return app.config.ApiKey
}