mirror of
https://github.com/absmach/magistrala.git
synced 2026-06-23 04:10:28 +00:00
NOISSUE - Fix provision configuration loading (#2078)
Signed-off-by: Arvindh <arvindh91@gmail.com>
This commit is contained in:
+14
-18
@@ -135,26 +135,22 @@ func loadConfig() (provision.Config, error) {
|
||||
return provision.Config{}, errFailedToReadBootstrapContent
|
||||
}
|
||||
}
|
||||
cfg.Bootstrap.Content = content
|
||||
|
||||
cfg = provision.Config{
|
||||
Bootstrap: provision.Bootstrap{
|
||||
Content: content,
|
||||
cfg.Channels = []mggroups.Group{
|
||||
{
|
||||
Name: "control-channel",
|
||||
Metadata: map[string]interface{}{"type": "control"},
|
||||
}, {
|
||||
Name: "data-channel",
|
||||
Metadata: map[string]interface{}{"type": "data"},
|
||||
},
|
||||
// This is default conf for provision if there is no config file
|
||||
Channels: []mggroups.Group{
|
||||
{
|
||||
Name: "control-channel",
|
||||
Metadata: map[string]interface{}{"type": "control"},
|
||||
}, {
|
||||
Name: "data-channel",
|
||||
Metadata: map[string]interface{}{"type": "data"},
|
||||
},
|
||||
},
|
||||
Things: []mgclients.Client{
|
||||
{
|
||||
Name: "thing",
|
||||
Metadata: map[string]interface{}{"external_id": "xxxxxx"},
|
||||
},
|
||||
}
|
||||
|
||||
cfg.Things = []mgclients.Client{
|
||||
{
|
||||
Name: "thing",
|
||||
Metadata: map[string]interface{}{"external_id": "xxxxxx"},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,6 @@ services:
|
||||
MG_PROVISION_CERTS_SVC_URL: ${MG_PROVISION_CERTS_SVC_URL}
|
||||
MG_PROVISION_X509_PROVISIONING: ${MG_PROVISION_X509_PROVISIONING}
|
||||
MG_PROVISION_BS_SVC_URL: ${MG_PROVISION_BS_SVC_URL}
|
||||
MG_PROVISION_BS_SVC_WHITELIST_URL: ${MG_PROVISION_BS_SVC_WHITELIST_URL}
|
||||
MG_PROVISION_BS_CONFIG_PROVISIONING: ${MG_PROVISION_BS_CONFIG_PROVISIONING}
|
||||
MG_PROVISION_BS_AUTO_WHITELIST: ${MG_PROVISION_BS_AUTO_WHITELIST}
|
||||
MG_PROVISION_BS_CONTENT: ${MG_PROVISION_BS_CONTENT}
|
||||
|
||||
+3
-4
@@ -30,9 +30,8 @@ default values.
|
||||
| MG_PROVISION_SERVER_KEY | Magistrala gRPC secure server key | |
|
||||
| MG_PROVISION_USERS_LOCATION | Users service URL | <http://users:9002> |
|
||||
| MG_PROVISION_THINGS_LOCATION | Things service URL | <http://things:9000> |
|
||||
| MG_PROVISION_BS_SVC_URL | Magistrala Bootstrap service URL | <http://bootstrap:9013/things> |
|
||||
| MG_PROVISION_BS_SVC_WHITELIST_URL | Magistrala Bootstrap service whitelist URL | <http://bootstrap:9013/things/state> |
|
||||
| MG_PROVISION_CERTS_SVC_URL | Certificates service URL | <http://certs:9019/certs> |
|
||||
| MG_PROVISION_BS_SVC_URL | Magistrala Bootstrap service URL | <http://bootstrap:9013> |
|
||||
| MG_PROVISION_CERTS_SVC_URL | Certificates service URL | <http://certs:9019> |
|
||||
| MG_PROVISION_X509_PROVISIONING | Should X509 client cert be provisioned | false |
|
||||
| MG_PROVISION_BS_CONFIG_PROVISIONING | Should thing config be saved in Bootstrap service | true |
|
||||
| MG_PROVISION_BS_AUTO_WHITELIST | Should thing be auto whitelisted | true |
|
||||
@@ -99,7 +98,7 @@ Provision service can be run as a standalone or in docker composition as addon t
|
||||
Standalone:
|
||||
|
||||
```bash
|
||||
MG_PROVISION_BS_SVC_URL=http://localhost:9013/things \
|
||||
MG_PROVISION_BS_SVC_URL=http://localhost:9013 \
|
||||
MG_PROVISION_THINGS_LOCATION=http://localhost:9000 \
|
||||
MG_PROVISION_USERS_LOCATION=http://localhost:9002 \
|
||||
MG_PROVISION_CONFIG_FILE=docker/addons/provision/configs/config.toml \
|
||||
|
||||
@@ -44,6 +44,11 @@ func getMapping(svc provision.Service) endpoint.Endpoint {
|
||||
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
||||
}
|
||||
|
||||
return svc.Mapping(req.token)
|
||||
res, err := svc.Mapping(req.token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return mappingRes{Data: res}, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,10 @@ func (req provisionReq) validate() error {
|
||||
return apiutil.ErrBearerKey
|
||||
}
|
||||
|
||||
if req.Name == "" {
|
||||
return apiutil.ErrMissingName
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/absmach/magistrala"
|
||||
@@ -32,3 +33,23 @@ func (res provisionRes) Headers() map[string]string {
|
||||
func (res provisionRes) Empty() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
type mappingRes struct {
|
||||
Data interface{}
|
||||
}
|
||||
|
||||
func (res mappingRes) Code() int {
|
||||
return http.StatusOK
|
||||
}
|
||||
|
||||
func (res mappingRes) Headers() map[string]string {
|
||||
return map[string]string{}
|
||||
}
|
||||
|
||||
func (res mappingRes) Empty() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (res mappingRes) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(res.Data)
|
||||
}
|
||||
|
||||
+14
-16
@@ -17,21 +17,20 @@ var errFailedToReadConfig = errors.New("failed to read config file")
|
||||
|
||||
// ServiceConf represents service config.
|
||||
type ServiceConf struct {
|
||||
Port string `toml:"port" env:"MG_PROVISION_HTTP_PORT" envDefault:"9016"`
|
||||
LogLevel string `toml:"log_level" env:"MG_PROVISION_LOG_LEVEL" envDefault:"info"`
|
||||
TLS bool `toml:"tls" env:"MG_PROVISION_ENV_CLIENTS_TLS" envDefault:"false"`
|
||||
ServerCert string `toml:"server_cert" env:"MG_PROVISION_SERVER_CERT" envDefault:""`
|
||||
ServerKey string `toml:"server_key" env:"MG_PROVISION_SERVER_KEY" envDefault:""`
|
||||
ThingsURL string `toml:"things_url" env:"MG_PROVISION_THINGS_LOCATION" envDefault:"http://localhost"`
|
||||
UsersURL string `toml:"users_url" env:"MG_PROVISION_USERS_LOCATION" envDefault:"http://localhost"`
|
||||
HTTPPort string `toml:"http_port" env:"MG_PROVISION_HTTP_PORT" envDefault:"9016"`
|
||||
MgUser string `toml:"mg_user" env:"MG_PROVISION_USER" envDefault:"test@example.com"`
|
||||
MgPass string `toml:"mg_pass" env:"MG_PROVISION_PASS" envDefault:"test"`
|
||||
MgDomainID string `toml:"mg_domain_id" env:"MG_PROVISION_DOMAIN_ID" envDefault:""`
|
||||
MgAPIKey string `toml:"mg_api_key" env:"MG_PROVISION_API_KEY" envDefault:""`
|
||||
MgBSURL string `toml:"mg_bs_url" env:"MG_PROVISION_BS_SVC_URL" envDefault:"http://localhost:9000/things/configs"`
|
||||
MgWhiteListURL string `toml:"mg_white_list" env:"MG_PROVISION_BS_SVC_WHITELIST_URL" envDefault:"http://localhost:9000/things/state"`
|
||||
MgCertsURL string `toml:"mg_certs_url" env:"MG_PROVISION_CERTS_SVC_URL" envDefault:"http://localhost:9019"`
|
||||
Port string `toml:"port" env:"MG_PROVISION_HTTP_PORT" envDefault:"9016"`
|
||||
LogLevel string `toml:"log_level" env:"MG_PROVISION_LOG_LEVEL" envDefault:"info"`
|
||||
TLS bool `toml:"tls" env:"MG_PROVISION_ENV_CLIENTS_TLS" envDefault:"false"`
|
||||
ServerCert string `toml:"server_cert" env:"MG_PROVISION_SERVER_CERT" envDefault:""`
|
||||
ServerKey string `toml:"server_key" env:"MG_PROVISION_SERVER_KEY" envDefault:""`
|
||||
ThingsURL string `toml:"things_url" env:"MG_PROVISION_THINGS_LOCATION" envDefault:"http://localhost"`
|
||||
UsersURL string `toml:"users_url" env:"MG_PROVISION_USERS_LOCATION" envDefault:"http://localhost"`
|
||||
HTTPPort string `toml:"http_port" env:"MG_PROVISION_HTTP_PORT" envDefault:"9016"`
|
||||
MgUser string `toml:"mg_user" env:"MG_PROVISION_USER" envDefault:"test@example.com"`
|
||||
MgPass string `toml:"mg_pass" env:"MG_PROVISION_PASS" envDefault:"test"`
|
||||
MgDomainID string `toml:"mg_domain_id" env:"MG_PROVISION_DOMAIN_ID" envDefault:""`
|
||||
MgAPIKey string `toml:"mg_api_key" env:"MG_PROVISION_API_KEY" envDefault:""`
|
||||
MgBSURL string `toml:"mg_bs_url" env:"MG_PROVISION_BS_SVC_URL" envDefault:"http://localhost:9000"`
|
||||
MgCertsURL string `toml:"mg_certs_url" env:"MG_PROVISION_CERTS_SVC_URL" envDefault:"http://localhost:9019"`
|
||||
}
|
||||
|
||||
// Bootstrap represetns the Bootstrap config.
|
||||
@@ -60,7 +59,6 @@ type Cert struct {
|
||||
|
||||
// Config struct of Provision.
|
||||
type Config struct {
|
||||
LogLevel string `toml:"log_level" env:"MG_PROVISION_LOG_LEVEL" envDefault:"info"`
|
||||
File string `toml:"file" env:"MG_PROVISION_CONFIG_FILE" envDefault:"config.toml"`
|
||||
Server ServiceConf `toml:"server" mapstructure:"server"`
|
||||
Bootstrap Bootstrap `toml:"bootstrap" mapstructure:"bootstrap"`
|
||||
|
||||
@@ -18,7 +18,6 @@ import (
|
||||
|
||||
var (
|
||||
validConfig = provision.Config{
|
||||
LogLevel: "info",
|
||||
Server: provision.ServiceConf{
|
||||
Port: "9016",
|
||||
LogLevel: "info",
|
||||
|
||||
@@ -16,10 +16,9 @@ file = "config.toml"
|
||||
http_port = "8190"
|
||||
mg_api_key = ""
|
||||
mg_bs_url = "http://localhost:9013"
|
||||
mg_certs_url = "http://localhost:9019/certs"
|
||||
mg_certs_url = "http://localhost:9019"
|
||||
mg_pass = ""
|
||||
mg_user = ""
|
||||
mg_white_list = "http://localhost:9013/things/state"
|
||||
mqtt_url = ""
|
||||
port = ""
|
||||
server_cert = ""
|
||||
@@ -46,4 +45,3 @@ file = "config.toml"
|
||||
|
||||
[channels.metadata]
|
||||
type = "data"
|
||||
|
||||
|
||||
@@ -158,7 +158,7 @@ func (ps *provisionService) Provision(token, name, externalID, externalKey strin
|
||||
|
||||
for _, channel := range ps.conf.Channels {
|
||||
ch := sdk.Channel{
|
||||
Name: channel.Name,
|
||||
Name: name + "_" + channel.Name,
|
||||
Metadata: sdk.Metadata(channel.Metadata),
|
||||
}
|
||||
ch, err := ps.sdk.CreateChannel(ch, token)
|
||||
@@ -340,11 +340,11 @@ func (ps *provisionService) errLog(err error) {
|
||||
|
||||
func clean(ps *provisionService, things []sdk.Thing, channels []sdk.Channel, token string) {
|
||||
for _, t := range things {
|
||||
_, err := ps.sdk.DisableThing(t.ID, token)
|
||||
err := ps.sdk.DeleteThing(t.ID, token)
|
||||
ps.errLog(err)
|
||||
}
|
||||
for _, c := range channels {
|
||||
_, err := ps.sdk.DisableChannel(c.ID, token)
|
||||
err := ps.sdk.DeleteChannel(c.ID, token)
|
||||
ps.errLog(err)
|
||||
}
|
||||
}
|
||||
@@ -357,7 +357,7 @@ func (ps *provisionService) recover(e *error, ths *[]sdk.Thing, chs *[]sdk.Chann
|
||||
|
||||
if errors.Contains(err, ErrFailedThingRetrieval) || errors.Contains(err, ErrFailedChannelCreation) {
|
||||
for _, th := range things {
|
||||
_, err := ps.sdk.DisableThing(th.ID, token)
|
||||
err := ps.sdk.DeleteThing(th.ID, token)
|
||||
ps.errLog(err)
|
||||
}
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user