-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added app instance
- Loading branch information
Showing
20 changed files
with
1,095 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package cloud | ||
|
||
import ( | ||
"errors" | ||
|
||
"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"` | ||
} | ||
|
||
// 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. | ||
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 | ||
} | ||
|
||
// 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, | ||
DeviceMessageHandler: app.config.DeviceMessageHandler, | ||
} | ||
} | ||
|
||
func (app *App) ID() string { | ||
return app.config.ID | ||
} | ||
|
||
func (app *App) ApiKey() string { | ||
return app.config.ApiKey | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
app: | ||
id: replace_with_app_id | ||
apiKey: replace_with_api_key | ||
globalFQDN: dnaservices.cisco.com | ||
regionalFQDN: neoffers.cisco.com | ||
readStream: replace_with_read_stream | ||
writeStream: replace_with_write_stream | ||
|
||
appInstance: | ||
otp: replace_with_otp_from_dna_cloud | ||
name: Instance 001 | ||
# config file will be rewritten with id, key after using OTP | ||
id: | ||
apiKey: | ||
tenant: | ||
id: | ||
name: | ||
token: |
Oops, something went wrong.