-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass sub_accounts support (#57)
* added framework for sub-accounts * sub_account create * added - create sub account - delete sub account - list sub accounts - get sub account and unit tests (and some API oddities) * added - update and unit test * changes to deal with new client New func
- Loading branch information
1 parent
24301f9
commit 14493ee
Showing
19 changed files
with
821 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package sub_accounts | ||
|
||
import ( | ||
"fmt" | ||
"github.com/jonboydell/logzio_client/client" | ||
) | ||
|
||
const ( | ||
subAccountServiceEndpoint = "%s/v1/account-management/time-based-accounts" | ||
) | ||
|
||
const ( | ||
fldAccountId string = "accountId" //required | ||
fldEmail string = "email" //required | ||
fldAccountName string = "accountName" //required | ||
fldMaxDailyGB string = "maxDailyGB" | ||
fldRetentionDays string = "retentionDays" //required | ||
fldAccessible string = "accessible" | ||
fldSearchable string = "searchable" | ||
fldSharingAccountObjects string = "sharingObjectsAccounts" //required | ||
fldDocSizeSetting string = "docSizeSetting" | ||
fldUtilizationSettings string = "utilizationSettings" | ||
fldFrequencyMinutes string = "frequencyMinutes" | ||
fldUtilizationEnabled string = "utilizationEnabled" | ||
fldAccountToken string = "accountToken" | ||
fldDailyUsagesList string = "dailyUsagesList" | ||
) | ||
|
||
type SubAccount struct { | ||
Id int64 | ||
Email string | ||
AccountName string | ||
MaxDailyGB float32 | ||
RetentionDays int32 | ||
Searchable bool | ||
Accessible bool | ||
SharingObjectAccounts []interface{} | ||
DocSizeSetting bool | ||
UtilizationSettings map[string]interface{} | ||
AccountToken string | ||
DailyUsagesList interface{} | ||
} | ||
|
||
type SubAccountClient struct { | ||
*client.Client | ||
} | ||
|
||
// Creates a new entry point into the sub-account functions, accepts the user's logz.io API token and account Id | ||
func New(apiToken string, baseUrl string) (*SubAccountClient, error) { | ||
if len(apiToken) == 0 { | ||
return nil, fmt.Errorf("API token not defined") | ||
} | ||
if len(baseUrl) == 0 { | ||
return nil, fmt.Errorf("Base URL not defined") | ||
} | ||
|
||
c := &SubAccountClient{ | ||
Client: client.New(apiToken, baseUrl), | ||
} | ||
return c, nil | ||
} | ||
|
||
func jsonToSubAccount(json map[string]interface{}) SubAccount { | ||
subAccount := SubAccount{ | ||
Id: int64(json[fldAccountId].(float64)), | ||
Email: json[fldEmail].(string), | ||
AccountName: json[fldAccountName].(string), | ||
AccountToken: json[fldAccountToken].(string), | ||
MaxDailyGB: float32(json[fldMaxDailyGB].(float64)), | ||
RetentionDays: int32(json[fldRetentionDays].(float64)), | ||
Searchable: json[fldSearchable].(bool), | ||
Accessible: json[fldAccessible].(bool), | ||
DocSizeSetting: json[fldDocSizeSetting].(bool), | ||
SharingObjectAccounts: json[fldSharingAccountObjects].([]interface{}), | ||
UtilizationSettings: json[fldUtilizationSettings].(map[string]interface{}), | ||
DailyUsagesList: json[fldDailyUsagesList], | ||
} | ||
return subAccount | ||
} |
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 sub_accounts | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/jonboydell/logzio_client" | ||
"github.com/jonboydell/logzio_client/client" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
serviceUrl string = subAccountServiceEndpoint | ||
serviceMethod string = http.MethodPost | ||
serviceSuccess int = http.StatusOK | ||
) | ||
|
||
func (c *SubAccountClient) createValidateRequest(s SubAccount) (error, bool) { | ||
return nil, true | ||
} | ||
|
||
func (c *SubAccountClient) createApiRequest(apiToken string, s SubAccount) (*http.Request, error) { | ||
var ( | ||
createUser = map[string]interface{}{ | ||
"email": s.Email, | ||
"accountName": s.AccountName, | ||
"maxDailyGB": s.MaxDailyGB, | ||
"retentionDays": s.RetentionDays, | ||
"searchable": s.Searchable, | ||
"accessible": s.Accessible, | ||
"sharingObjectsAccounts": s.SharingObjectAccounts, | ||
"docSizeSetting": s.DocSizeSetting, | ||
"utilizationSettings": s.UtilizationSettings, | ||
} | ||
) | ||
|
||
jsonBytes, err := json.Marshal(createUser) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
baseUrl := c.BaseUrl | ||
url := fmt.Sprintf(serviceUrl, baseUrl) | ||
req, err := http.NewRequest(serviceMethod, url, bytes.NewBuffer(jsonBytes)) | ||
logzio_client.AddHttpHeaders(apiToken, req) | ||
|
||
return req, err | ||
} | ||
|
||
func (c *SubAccountClient) createHttpRequest(req *http.Request) (map[string]interface{}, error) { | ||
httpClient := client.GetHttpClient(req) | ||
resp, err := httpClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
jsonBytes, err := ioutil.ReadAll(resp.Body) | ||
if !logzio_client.CheckValidStatus(resp, []int{serviceSuccess}) { | ||
return nil, fmt.Errorf("%d %s", resp.StatusCode, jsonBytes) | ||
} | ||
var target map[string]interface{} | ||
err = json.Unmarshal(jsonBytes, &target) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return target, nil | ||
} | ||
|
||
func (c *SubAccountClient) createCheckResponse(response map[string]interface{}) error { | ||
return nil | ||
} | ||
|
||
func (c *SubAccountClient) CreateSubAccount(subAccount SubAccount) (*SubAccount, error) { | ||
if err, ok := c.createValidateRequest(subAccount); !ok { | ||
return nil, err | ||
} | ||
req, _ := c.createApiRequest(c.ApiToken, subAccount) | ||
|
||
target, err := c.createHttpRequest(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = c.createCheckResponse(target) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
subAccount.Id = int64(target["accountId"].(float64)) | ||
return &subAccount, nil | ||
} |
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,58 @@ | ||
package sub_accounts | ||
|
||
import ( | ||
"fmt" | ||
"github.com/jonboydell/logzio_client" | ||
"github.com/jonboydell/logzio_client/client" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
deleteServiceUrl string = subAccountServiceEndpoint + "/%d" | ||
deleteServiceMethod string = http.MethodDelete | ||
deleteServiceSuccess int = http.StatusNoContent | ||
) | ||
|
||
func (c *SubAccountClient) deleteValidateRequest(id int64) (error, bool) { | ||
return nil, true | ||
} | ||
|
||
func (c *SubAccountClient) deleteApiRequest(apiToken string, id int64) (*http.Request, error) { | ||
url := fmt.Sprintf(deleteServiceUrl, c.BaseUrl, id) | ||
req, err := http.NewRequest(deleteServiceMethod, url, nil) | ||
logzio_client.AddHttpHeaders(apiToken, req) | ||
|
||
return req, err | ||
} | ||
|
||
func (c *SubAccountClient) deleteHttpRequest(req *http.Request) error { | ||
httpClient := client.GetHttpClient(req) | ||
resp, err := httpClient.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
if !logzio_client.CheckValidStatus(resp, []int{deleteServiceSuccess}) { | ||
jsonBytes, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
return fmt.Errorf("%d %s", resp.StatusCode, jsonBytes) | ||
} | ||
return nil | ||
} | ||
|
||
func (c *SubAccountClient) DeleteSubAccount(id int64) (error) { | ||
if err, ok := c.deleteValidateRequest(id); !ok { | ||
return err | ||
} | ||
req, _ := c.deleteApiRequest(c.ApiToken, id) | ||
|
||
err := c.deleteHttpRequest(req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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,71 @@ | ||
package sub_accounts | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/jonboydell/logzio_client" | ||
"github.com/jonboydell/logzio_client/client" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
getServiceUrl string = subAccountServiceEndpoint + "/%d" | ||
getServiceMethod string = http.MethodGet | ||
getServiceSuccess int = http.StatusOK | ||
) | ||
|
||
func (c *SubAccountClient) getValidateRequest(id int64) (error, bool) { | ||
return nil, true | ||
} | ||
|
||
func (c *SubAccountClient) getApiRequest(apiToken string, id int64) (*http.Request, error) { | ||
|
||
url := fmt.Sprintf(getServiceUrl, c.BaseUrl, id) | ||
req, err := http.NewRequest(getServiceMethod, url, nil) | ||
logzio_client.AddHttpHeaders(apiToken, req) | ||
return req, err | ||
} | ||
|
||
func (c *SubAccountClient) getHttpRequest(req *http.Request) (map[string]interface{}, error) { | ||
httpClient := client.GetHttpClient(req) | ||
resp, err := httpClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
jsonBytes, err := ioutil.ReadAll(resp.Body) | ||
if !logzio_client.CheckValidStatus(resp, []int{getServiceSuccess}) { | ||
return nil, fmt.Errorf("%d %s", resp.StatusCode, jsonBytes) | ||
} | ||
var target map[string]interface{} | ||
err = json.Unmarshal(jsonBytes, &target) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return target, nil | ||
} | ||
|
||
func (c *SubAccountClient) getCheckResponse(response map[string]interface{}) error { | ||
return nil | ||
} | ||
|
||
func (c *SubAccountClient) GetSubAccount(id int64) (*SubAccount, error) { | ||
if err, ok := c.getValidateRequest(id); !ok { | ||
return nil, err | ||
} | ||
req, _ := c.getApiRequest(c.ApiToken, id) | ||
|
||
target, err := c.getHttpRequest(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = c.getCheckResponse(target) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
subAccount := jsonToSubAccount(target) | ||
return &subAccount, nil | ||
} |
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,73 @@ | ||
package sub_accounts | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/jonboydell/logzio_client" | ||
"github.com/jonboydell/logzio_client/client" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
listServiceUrl string = subAccountServiceEndpoint | ||
listServiceMethod string = http.MethodGet | ||
listServiceSuccess int = http.StatusOK | ||
) | ||
|
||
func (c *SubAccountClient) listValidateRequest(id int64) (error, bool) { | ||
return nil, true | ||
} | ||
|
||
func (c *SubAccountClient) listApiRequest(apiToken string) (*http.Request, error) { | ||
url := fmt.Sprintf(listServiceUrl, c.BaseUrl) | ||
req, err := http.NewRequest(listServiceMethod, url, nil) | ||
logzio_client.AddHttpHeaders(apiToken, req) | ||
return req, err | ||
} | ||
|
||
func (c *SubAccountClient) listHttpRequest(req *http.Request) ([]map[string]interface{}, error) { | ||
httpClient := client.GetHttpClient(req) | ||
resp, err := httpClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
jsonBytes, err := ioutil.ReadAll(resp.Body) | ||
if !logzio_client.CheckValidStatus(resp, []int{listServiceSuccess}) { | ||
return nil, fmt.Errorf("%d %s", resp.StatusCode, jsonBytes) | ||
} | ||
var target []map[string]interface{} | ||
err = json.Unmarshal(jsonBytes, &target) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return target, nil | ||
} | ||
|
||
func (c *SubAccountClient) listCheckResponse(response []map[string]interface{}) error { | ||
return nil | ||
} | ||
|
||
func (c *SubAccountClient) ListSubAccounts() ([]SubAccount, error) { | ||
|
||
req, _ := c.listApiRequest(c.ApiToken) | ||
|
||
target, err := c.listHttpRequest(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = c.listCheckResponse(target) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var subAccounts []SubAccount | ||
for _, json := range target { | ||
subAccount := jsonToSubAccount(json) | ||
subAccounts = append(subAccounts, subAccount) | ||
} | ||
|
||
return subAccounts, nil | ||
} |
Oops, something went wrong.