mirror of
https://github.com/absmach/magistrala.git
synced 2026-06-23 04:10:28 +00:00
MG-3512 - Add rendered context field to update endpoint (#3513)
Property Based Tests / api-test (push) Has been cancelled
Continuous Delivery / lint-and-build (push) Has been cancelled
Deploy GitHub Pages / swagger-ui (push) Has been cancelled
CI Pipeline / Lint Proto (push) Has been cancelled
CI Pipeline / Detect Changes (push) Has been cancelled
Continuous Delivery / Build and Push Docker Images (push) Has been cancelled
CI Pipeline / lint-and-build (push) Has been cancelled
CI Pipeline / Test ${{ matrix.module }} (push) Has been cancelled
CI Pipeline / Upload Coverage (push) Has been cancelled
Property Based Tests / api-test (push) Has been cancelled
Continuous Delivery / lint-and-build (push) Has been cancelled
Deploy GitHub Pages / swagger-ui (push) Has been cancelled
CI Pipeline / Lint Proto (push) Has been cancelled
CI Pipeline / Detect Changes (push) Has been cancelled
Continuous Delivery / Build and Push Docker Images (push) Has been cancelled
CI Pipeline / lint-and-build (push) Has been cancelled
CI Pipeline / Test ${{ matrix.module }} (push) Has been cancelled
CI Pipeline / Upload Coverage (push) Has been cancelled
Signed-off-by: nyagamunene <stevenyaga2014@gmail.com>
This commit is contained in:
@@ -133,9 +133,10 @@ func updateEndpoint(svc bootstrap.Service) endpoint.Endpoint {
|
||||
}
|
||||
|
||||
config := bootstrap.Config{
|
||||
ID: req.id,
|
||||
Name: req.Name,
|
||||
Content: req.Content,
|
||||
ID: req.id,
|
||||
Name: req.Name,
|
||||
Content: req.Content,
|
||||
RenderContext: req.RenderContext,
|
||||
}
|
||||
|
||||
if err := svc.Update(ctx, session, config); err != nil {
|
||||
|
||||
@@ -65,15 +65,17 @@ var (
|
||||
}
|
||||
|
||||
updateReq = struct {
|
||||
Content string `json:"content,omitempty"`
|
||||
Status bootstrap.Status `json:"status,omitempty"`
|
||||
ClientCert string `json:"client_cert,omitempty"`
|
||||
CACert string `json:"ca_cert,omitempty"`
|
||||
Content string `json:"content,omitempty"`
|
||||
Status bootstrap.Status `json:"status,omitempty"`
|
||||
ClientCert string `json:"client_cert,omitempty"`
|
||||
CACert string `json:"ca_cert,omitempty"`
|
||||
RenderContext map[string]any `json:"render_context,omitempty"`
|
||||
}{
|
||||
Content: "config update",
|
||||
Status: bootstrap.EnabledStatus,
|
||||
ClientCert: "newcert",
|
||||
CACert: "newca",
|
||||
Content: "config update",
|
||||
Status: bootstrap.EnabledStatus,
|
||||
ClientCert: "newcert",
|
||||
CACert: "newca",
|
||||
RenderContext: map[string]any{"site": "warehouse-2", "region": "mombasa"},
|
||||
}
|
||||
|
||||
missingIDRes = toJSON(apiutil.ErrMissingID)
|
||||
@@ -478,6 +480,17 @@ func TestUpdate(t *testing.T) {
|
||||
status: http.StatusBadRequest,
|
||||
err: svcerr.ErrMalformedEntity,
|
||||
},
|
||||
{
|
||||
desc: "update a config render_context",
|
||||
req: toJSON(struct {
|
||||
RenderContext map[string]any `json:"render_context"`
|
||||
}{RenderContext: map[string]any{"site": "warehouse-2", "region": "mombasa"}}),
|
||||
id: c.ID,
|
||||
token: validToken,
|
||||
contentType: contentType,
|
||||
status: http.StatusOK,
|
||||
err: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
|
||||
@@ -52,9 +52,10 @@ func (req entityReq) validate() error {
|
||||
}
|
||||
|
||||
type updateReq struct {
|
||||
id string
|
||||
Name string `json:"name"`
|
||||
Content string `json:"content"`
|
||||
id string
|
||||
Name string `json:"name"`
|
||||
Content string `json:"content"`
|
||||
RenderContext map[string]any `json:"render_context"`
|
||||
}
|
||||
|
||||
func (req updateReq) validate() error {
|
||||
|
||||
@@ -169,13 +169,19 @@ func (cr configRepository) RetrieveByExternalID(ctx context.Context, externalID
|
||||
}
|
||||
|
||||
func (cr configRepository) Update(ctx context.Context, cfg bootstrap.Config) error {
|
||||
q := `UPDATE configs SET name = :name, content = :content WHERE id = :id AND domain_id = :domain_id `
|
||||
q := `UPDATE configs SET name = :name, content = :content, render_context = :render_context WHERE id = :id AND domain_id = :domain_id `
|
||||
|
||||
renderContext, err := json.Marshal(cfg.RenderContext)
|
||||
if err != nil {
|
||||
return errors.Wrap(repoerr.ErrUpdateEntity, err)
|
||||
}
|
||||
|
||||
dbcfg := dbConfig{
|
||||
Name: nullString(cfg.Name),
|
||||
Content: nullString(cfg.Content),
|
||||
ID: cfg.ID,
|
||||
DomainID: cfg.DomainID,
|
||||
Name: nullString(cfg.Name),
|
||||
Content: nullString(cfg.Content),
|
||||
RenderContext: renderContext,
|
||||
ID: cfg.ID,
|
||||
DomainID: cfg.DomainID,
|
||||
}
|
||||
|
||||
res, err := cr.db.NamedExecContext(ctx, q, dbcfg)
|
||||
|
||||
@@ -256,17 +256,23 @@ func TestUpdate(t *testing.T) {
|
||||
c.Content = "new content"
|
||||
c.Name = "new name"
|
||||
|
||||
withRenderContext := c
|
||||
withRenderContext.RenderContext = map[string]any{
|
||||
"site": "warehouse-2",
|
||||
"region": "mombasa",
|
||||
}
|
||||
|
||||
wrongDomainID := c
|
||||
wrongDomainID.DomainID = "3"
|
||||
|
||||
cases := []struct {
|
||||
desc string
|
||||
id string
|
||||
config bootstrap.Config
|
||||
err error
|
||||
desc string
|
||||
config bootstrap.Config
|
||||
renderContext map[string]any
|
||||
err error
|
||||
}{
|
||||
{
|
||||
desc: "update with wrong domainID ",
|
||||
desc: "update with wrong domainID",
|
||||
config: wrongDomainID,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
@@ -275,10 +281,21 @@ func TestUpdate(t *testing.T) {
|
||||
config: c,
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
desc: "update a config render_context",
|
||||
config: withRenderContext,
|
||||
renderContext: map[string]any{"site": "warehouse-2", "region": "mombasa"},
|
||||
err: nil,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
err := repo.Update(context.Background(), tc.config)
|
||||
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
||||
if tc.err == nil && tc.renderContext != nil {
|
||||
saved, err := repo.RetrieveByID(context.Background(), tc.config.DomainID, tc.config.ID)
|
||||
require.Nil(t, err, fmt.Sprintf("%s: unexpected retrieve error: %s\n", tc.desc, err))
|
||||
assert.Equal(t, tc.renderContext, saved.RenderContext, fmt.Sprintf("%s: expected render_context %v got %v\n", tc.desc, tc.renderContext, saved.RenderContext))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -199,6 +199,12 @@ func TestUpdate(t *testing.T) {
|
||||
modifiedCreated.Content = "new-config"
|
||||
modifiedCreated.Name = "new name"
|
||||
|
||||
modifiedRenderContext := c
|
||||
modifiedRenderContext.RenderContext = map[string]any{
|
||||
"site": "warehouse-2",
|
||||
"region": "mombasa",
|
||||
}
|
||||
|
||||
nonExisting := c
|
||||
nonExisting.ID = unknown
|
||||
|
||||
@@ -220,6 +226,14 @@ func TestUpdate(t *testing.T) {
|
||||
domainID: domainID,
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
desc: "update a config render_context",
|
||||
config: modifiedRenderContext,
|
||||
token: validToken,
|
||||
userID: validID,
|
||||
domainID: domainID,
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
desc: "update a non-existing config",
|
||||
config: nonExisting,
|
||||
|
||||
Reference in New Issue
Block a user