Skip to content

Commit

Permalink
NOISSUE - Fix provision configuration loading (#2078)
Browse files Browse the repository at this point in the history
Signed-off-by: Arvindh <arvindh91@gmail.com>
  • Loading branch information
arvindh123 authored Feb 19, 2024
1 parent a9e5897 commit 21c5813
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 49 deletions.
34 changes: 15 additions & 19 deletions cmd/provision/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,26 +135,22 @@ func loadConfig() (provision.Config, error) {
return provision.Config{}, errFailedToReadBootstrapContent
}
}

cfg = provision.Config{
Bootstrap: provision.Bootstrap{
Content: content,
},
// 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"},
},
cfg.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"},
},
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"},
},
}

Expand Down
1 change: 0 additions & 1 deletion docker/addons/provision/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
7 changes: 3 additions & 4 deletions provision/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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 \
Expand Down
7 changes: 6 additions & 1 deletion provision/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
4 changes: 4 additions & 0 deletions provision/api/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func (req provisionReq) validate() error {
return apiutil.ErrBearerKey
}

if req.Name == "" {
return apiutil.ErrMissingName
}

return nil
}

Expand Down
21 changes: 21 additions & 0 deletions provision/api/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package api

import (
"encoding/json"
"net/http"

"github.com/absmach/magistrala"
Expand Down Expand Up @@ -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)
}
30 changes: 14 additions & 16 deletions provision/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"`
Expand Down
1 change: 0 additions & 1 deletion provision/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (

var (
validConfig = provision.Config{
LogLevel: "info",
Server: provision.ServiceConf{
Port: "9016",
LogLevel: "info",
Expand Down
4 changes: 1 addition & 3 deletions provision/configs/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand All @@ -46,4 +45,3 @@ file = "config.toml"

[channels.metadata]
type = "data"

8 changes: 4 additions & 4 deletions provision/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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
Expand Down

0 comments on commit 21c5813

Please sign in to comment.