Exemple #1
0
func (a *App) UploadUrl(loginInfo *login.LoginInfo) (*url.URL, error) {
	type _urlPayload struct {
		Url string `json:"url"`
	}

	var urlPayload _urlPayload

	params := url.Values{
		"Authorization": []string{loginInfo.Token},
	}
	response, err := nestorclient.CallAPI(fmt.Sprintf("/teams/%s/powers/issue_upload_url", loginInfo.DefaultTeamId), "POST", params, 200)
	if err != nil {
		if ne, ok := err.(nestorclient.NestorAPIError); ok {
			return nil, ne
		}
		return nil, errors.UnexpectedServerError
	}

	if err = json.Unmarshal([]byte(response), &urlPayload); err != nil {
		// If JSON parsing fails that means it's a server error too
		return nil, errors.UnexpectedServerError
	}

	return url.Parse(urlPayload.Url)
}
Exemple #2
0
func (a *App) Hydrate(loginInfo *login.LoginInfo) error {
	var appFromServer App

	params := url.Values{
		"Authorization":  []string{loginInfo.Token},
		"app[permalink]": []string{a.Permalink},
	}

	response, err := nestorclient.CallAPI(fmt.Sprintf("/teams/%s/powers/search", loginInfo.DefaultTeamId), "GET", params, 200)

	if err != nil {
		if ne, ok := err.(nestorclient.NestorAPIError); ok {
			return ne
		}

		return errors.UnexpectedServerError
	}

	if err = json.Unmarshal([]byte(response), &appFromServer); err != nil {
		// If JSON parsing fails that means it's a server error too
		return errors.UnexpectedServerError
	}

	a.Id = appFromServer.Id
	a.RemoteSha256 = appFromServer.RemoteSha256

	return nil
}
Exemple #3
0
func Login(email string, password string) (*LoginInfo, error) {
	l := LoginInfo{Email: email}

	params := url.Values{
		"user[email]":    []string{email},
		"user[password]": []string{password},
	}

	response, err := nestorclient.CallAPI("/users/issue_token", "POST", params, 200)

	if err != nil {
		if ne, ok := err.(nestorclient.NestorAPIError); ok {
			return nil, ne
		}
		return nil, errors.UnexpectedServerError
	}

	if err = json.Unmarshal([]byte(response), &l); err != nil {
		// If JSON parsing fails that means it's a server error too
		return nil, errors.UnexpectedServerError
	}

	// Wrap the error from the API in an error struct
	if l.Err != "" {
		return nil, fmt.Errorf(l.Err)
	}

	return &l, nil
}
Exemple #4
0
func (a *App) SaveToNestor(l *login.LoginInfo) error {
	var returnedApp App
	var response string
	var err error

	params := url.Values{
		"Authorization":    []string{l.Token},
		"app[name]":        []string{a.Name},
		"app[description]": []string{a.Description},
		"app[permalink]":   []string{a.Permalink},
		"app[upload_key]":  []string{a.UploadKey},
		"app[sha_256]":     []string{a.LocalSha256},
	}

	if len(a.EnvironmentKeys) > 0 {
		envKeysPayload, err := json.Marshal(a.EnvironmentKeys)
		if err != nil {
			return err
		}

		params.Set("app[environment_keys]", string(envKeysPayload))
	}

	// If the app hasn't been created yet, then create it, otherwise update it
	if a.Id == 0 {
		response, err = nestorclient.CallAPI(fmt.Sprintf("/teams/%s/powers", l.DefaultTeamId), "POST", params, 201)
	} else {
		response, err = nestorclient.CallAPI(fmt.Sprintf("/teams/%s/powers/%s", l.DefaultTeamId, a.Permalink), "PATCH", params, 200)
	}

	if err != nil {
		if ne, ok := err.(nestorclient.NestorAPIError); ok {
			return ne
		}
		return errors.UnexpectedServerError
	}

	if err = json.Unmarshal([]byte(response), &returnedApp); err != nil {
		return errors.UnexpectedServerError
	}

	a.Id = returnedApp.Id

	return err
}
Exemple #5
0
func (v *Version) Deploy(a app.App, l *login.LoginInfo) error {
	params := url.Values{
		"Authorization": []string{l.Token},
		"version[ref]":  []string{v.Ref},
	}

	_, err := nestorclient.CallAPI(fmt.Sprintf("/teams/%s/powers/%s/deploys", l.DefaultTeamId, a.Permalink), "POST", params, 201)
	if err != nil {
		if ne, ok := err.(nestorclient.NestorAPIError); ok {
			return ne
		}
		return err
	}

	return nil
}
Exemple #6
0
func (a *App) GetEnv(l *login.LoginInfo, key string) (*tablewriter.Table, error) {
	params := url.Values{
		"Authorization": []string{l.Token},
	}
	responseEnv := map[string]string{}

	response, err := nestorclient.CallAPI(fmt.Sprintf("/teams/%s/environment", l.DefaultTeamId), "GET", params, 200)
	if err != nil {
		if ne, ok := err.(nestorclient.NestorAPIError); ok {
			return nil, ne
		}
		return nil, err
	}
	err = json.Unmarshal([]byte(response), &responseEnv)

	if err != nil {
		return nil, err
	}

	return a.formattedEnv(responseEnv, key), err
}
Exemple #7
0
func FetchVersions(a app.App, l *login.LoginInfo) ([]Version, error) {
	params := url.Values{
		"Authorization": []string{l.Token},
	}
	var versions []Version

	response, err := nestorclient.CallAPI(fmt.Sprintf("/teams/%s/powers/%s/versions", l.DefaultTeamId, a.Permalink), "GET", params, 200)
	if err != nil {
		if ne, ok := err.(nestorclient.NestorAPIError); ok {
			return nil, ne
		}
		return versions, err
	}

	if err = json.Unmarshal([]byte(response), &versions); err != nil {
		// If JSON parsing fails that means it's a server error too
		return versions, errors.UnexpectedServerError
	}

	return versions, nil
}
Exemple #8
0
func GetTeams(loginInfo *login.LoginInfo) ([]Team, error) {
	var teams = []Team{}

	params := url.Values{
		"Authorization": []string{loginInfo.Token},
	}

	response, err := nestorclient.CallAPI("/teams", "GET", params, 200)

	if err != nil {
		if ne, ok := err.(nestorclient.NestorAPIError); ok {
			return nil, ne
		}
		return nil, errors.UnexpectedServerError
	}

	if err = json.Unmarshal([]byte(response), &teams); err != nil {
		// If JSON parsing fails that means it's a server error too
		return nil, errors.UnexpectedServerError
	}

	return teams, nil
}
Exemple #9
0
func (a *App) UpdateEnv(l *login.LoginInfo, key string, val string) (string, error) {
	params := url.Values{
		"Authorization":                   []string{l.Token},
		"team[environment_hash][][name]":  []string{key},
		"team[environment_hash][][value]": []string{val},
	}
	responseEnv := map[string]string{}

	response, err := nestorclient.CallAPI(fmt.Sprintf("/teams/%s/update_environment", l.DefaultTeamId), "PATCH", params, 200)
	if err != nil {
		if ne, ok := err.(nestorclient.NestorAPIError); ok {
			return "", ne
		}
		return "", nil
	}

	err = json.Unmarshal([]byte(response), &responseEnv)
	if err != nil {
		return "", err
	}

	return responseEnv[key], err
}