1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-05-06 19:29:15 +08:00
Mainflux.mainflux/pkg/sdk/go/bootstrap.go
Aryan Godara e6e9d22133
MF-1670 - Improve error handling in SDK (#1674)
* initial commit

Signed-off-by: aryan <aryangodara03@gmail.com>

* remove unused variables.

Signed-off-by: aryan <aryangodara03@gmail.com>

* removed temporarily created file.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Fix failing CI

Signed-off-by: aryan <aryangodara03@gmail.com>

* Fix thing_test failing cases.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Remove dead code, debug statements, and add comments.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Extract errors to separate file.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Updated things/api/http tests

Signed-off-by: aryan <aryangodara03@gmail.com>

* Created custom SDK error.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Changed to using CheckError. All tests passing.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Replace error interface with errors.SDKError interface.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Fix failing CI.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Remove unused sdk errors.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Change SDKError to error in internal function of sdk package.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Remove unused error.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Remove encodeError. All tests working.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Rename sdkerr vars, convert common strings to constants.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Change checkerror to take error instead of string.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Remove unused errors, and removed errfailedwhitelist wrap.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Removed unused errors, and remove errors.go since it only had a repeated error from errors package

Signed-off-by: aryan <aryangodara03@gmail.com>

* Remove unused errors.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Update sdk_error.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Used function to reduce code for sending and receiving requests.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Added function sendrequestandgetheadersorerror.

Signed-off-by: aryan <aryangodara03@gmail.com>

* sdk_error updated.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Updated function names to processRequest.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Made errors internal, fixed typo in http.

Signed-off-by: aryan <aryangodara03@gmail.com>

* Remove empty line.

Signed-off-by: aryan <aryangodara03@gmail.com>

* merged proceessBody and processHeaders functions in sdk.

Signed-off-by: aryan <aryangodara03@gmail.com>

* remove sendThingRequest function.

Signed-off-by: aryan <aryangodara03@gmail.com>

* changed processRequest signature

Signed-off-by: aryan <aryangodara03@gmail.com>

* changed processRequest signature, changed error names.

Signed-off-by: aryan <aryangodara03@gmail.com>

Signed-off-by: aryan <aryangodara03@gmail.com>
Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
2022-12-15 16:24:19 +01:00

142 lines
4.5 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package sdk
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/mainflux/mainflux/pkg/errors"
)
const configsEndpoint = "configs"
const bootstrapEndpoint = "bootstrap"
const whitelistEndpoint = "state"
const bootstrapCertsEndpoint = "configs/certs"
// BootstrapConfig represents Configuration entity. It wraps information about external entity
// as well as info about corresponding Mainflux entities.
// MFThing represents corresponding Mainflux Thing ID.
// MFKey is key of corresponding Mainflux Thing.
// MFChannels is a list of Mainflux Channels corresponding Mainflux Thing connects to.
type BootstrapConfig struct {
ThingID string `json:"thing_id,omitempty"`
Channels []string `json:"channels,omitempty"`
ExternalID string `json:"external_id,omitempty"`
ExternalKey string `json:"external_key,omitempty"`
MFThing string `json:"mainflux_id,omitempty"`
MFChannels []Channel `json:"mainflux_channels,omitempty"`
MFKey string `json:"mainflux_key,omitempty"`
Name string `json:"name,omitempty"`
ClientCert string `json:"client_cert,omitempty"`
ClientKey string `json:"client_key,omitempty"`
CACert string `json:"ca_cert,omitempty"`
Content string `json:"content,omitempty"`
State int `json:"state,omitempty"`
}
type ConfigUpdateCertReq struct {
ClientCert string `json:"client_cert"`
ClientKey string `json:"client_key"`
CACert string `json:"ca_cert"`
}
func (sdk mfSDK) AddBootstrap(token string, cfg BootstrapConfig) (string, errors.SDKError) {
data, err := json.Marshal(cfg)
if err != nil {
return "", errors.NewSDKError(err)
}
url := fmt.Sprintf("%s/%s", sdk.bootstrapURL, configsEndpoint)
headers, _, sdkerr := sdk.processRequest(http.MethodPost, url, token, string(CTJSON), data, http.StatusOK, http.StatusCreated)
if sdkerr != nil {
return "", sdkerr
}
id := strings.TrimPrefix(headers.Get("Location"), "/things/configs/")
return id, nil
}
func (sdk mfSDK) Whitelist(token string, cfg BootstrapConfig) errors.SDKError {
data, err := json.Marshal(BootstrapConfig{State: cfg.State})
if err != nil {
return errors.NewSDKError(err)
}
if cfg.MFThing == "" {
return errors.NewSDKError(errors.ErrNotFoundParam)
}
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, whitelistEndpoint, cfg.MFThing)
_, _, sdkerr := sdk.processRequest(http.MethodPut, url, token, string(CTJSON), data, http.StatusCreated, http.StatusOK)
return sdkerr
}
func (sdk mfSDK) ViewBootstrap(token, id string) (BootstrapConfig, errors.SDKError) {
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, configsEndpoint, id)
_, body, err := sdk.processRequest(http.MethodGet, url, token, string(CTJSON), nil, http.StatusOK)
if err != nil {
return BootstrapConfig{}, err
}
var bc BootstrapConfig
if err := json.Unmarshal(body, &bc); err != nil {
return BootstrapConfig{}, errors.NewSDKError(err)
}
return bc, nil
}
func (sdk mfSDK) UpdateBootstrap(token string, cfg BootstrapConfig) errors.SDKError {
data, err := json.Marshal(cfg)
if err != nil {
return errors.NewSDKError(err)
}
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, configsEndpoint, cfg.MFThing)
_, _, sdkerr := sdk.processRequest(http.MethodPut, url, token, string(CTJSON), data, http.StatusOK)
return sdkerr
}
func (sdk mfSDK) UpdateBootstrapCerts(token, id, clientCert, clientKey, ca string) errors.SDKError {
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, bootstrapCertsEndpoint, id)
request := ConfigUpdateCertReq{
ClientCert: clientCert,
ClientKey: clientKey,
CACert: ca,
}
data, err := json.Marshal(request)
if err != nil {
return errors.NewSDKError(err)
}
_, _, sdkerr := sdk.processRequest(http.MethodPatch, url, token, string(CTJSON), data, http.StatusOK)
return sdkerr
}
func (sdk mfSDK) RemoveBootstrap(token, id string) errors.SDKError {
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, configsEndpoint, id)
_, _, err := sdk.processRequest(http.MethodDelete, url, token, string(CTJSON), nil, http.StatusNoContent)
return err
}
func (sdk mfSDK) Bootstrap(externalKey, externalID string) (BootstrapConfig, errors.SDKError) {
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, bootstrapEndpoint, externalID)
_, body, err := sdk.processRequest(http.MethodGet, url, externalKey, string(CTJSON), nil, http.StatusOK)
if err != nil {
return BootstrapConfig{}, err
}
var bc BootstrapConfig
if err := json.Unmarshal(body, &bc); err != nil {
return BootstrapConfig{}, errors.NewSDKError(err)
}
return bc, nil
}