Skip to content

Commit

Permalink
Merge pull request #180 from tranchitella/remove-provisioning-of-tenants
Browse files Browse the repository at this point in the history
Remove provisioning of tenants
  • Loading branch information
tranchitella committed Jan 13, 2022
2 parents d8f94b4 + f14b788 commit eeb8699
Show file tree
Hide file tree
Showing 14 changed files with 16 additions and 217 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2021 Northern.tech AS
Copyright 2022 Northern.tech AS

All content in this project is licensed under the Apache License v2, unless
indicated otherwise.
Expand Down
2 changes: 1 addition & 1 deletion LIC_FILES_CHKSUM.sha256
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Apache-2.0
b4acfcfa2a0ba1a8c82ec3965fbcee886cff8394ca4214e0ddac0a36beb1e05a LICENSE
1033348db7606a7e61b6484f293847cf8d7a35766efebb97e304d4bd5d7f3f6b LICENSE
b40930bbcf80744c86c46a12bc9da056641d722716c378f5659b9e555ef833e1 vendor/gopkg.in/yaml.v2/LICENSE
d18f6323b71b0b768bb5e9616e36da390fbd39369a81807cca352de4e4e6aa0b vendor/gopkg.in/yaml.v3/LICENSE
f6918bd93ffe07f4b2c61b8287c32cb3122e08aed0be50f1c7d0eddc87877a8f vendor/gopkg.in/ini.v1/LICENSE
Expand Down
38 changes: 1 addition & 37 deletions api/http/internal.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Northern.tech AS
// Copyright 2022 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -15,13 +15,11 @@
package http

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

"github.com/gin-gonic/gin"
"github.com/mendersoftware/go-lib-micro/ws"
"github.com/mendersoftware/go-lib-micro/ws/menderclient"
"github.com/pkg/errors"
"github.com/vmihailenco/msgpack/v5"

"github.com/mendersoftware/deviceconnect/app"
Expand All @@ -40,40 +38,6 @@ func NewInternalController(app app.App, nc nats.Client) *InternalController {
return &InternalController{app: app, nats: nc}
}

// Provision responds to POST /tenants
func (h InternalController) Provision(c *gin.Context) {
rawData, err := c.GetRawData()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "bad request",
})
return
}

tenant := &model.Tenant{}
if err = json.Unmarshal(rawData, tenant); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": errors.Wrap(err, "invalid payload").Error(),
})
return
} else if tenant.TenantID == "" {
c.JSON(http.StatusBadRequest, gin.H{
"error": "tenant_id is empty",
})
return
}

ctx := c.Request.Context()
if err = h.app.ProvisionTenant(ctx, tenant); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": errors.Wrap(err, "error provisioning the tenant").Error(),
})
return
}

c.Writer.WriteHeader(http.StatusCreated)
}

func (h InternalController) CheckUpdate(c *gin.Context) {
h.sendMenderCommand(c, menderclient.MessageTypeMenderClientCheckUpdate)
}
Expand Down
71 changes: 1 addition & 70 deletions api/http/internal_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Northern.tech AS
// Copyright 2022 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,75 +31,6 @@ import (
"github.com/mendersoftware/deviceconnect/model"
)

func TestProvision(t *testing.T) {
testCases := []struct {
Name string
TenantID string
Tenant string
ProvisionTenantErr error
HTTPStatus int
}{
{
Name: "ok",
TenantID: "1234",
Tenant: `{"tenant_id": "1234"}`,
HTTPStatus: http.StatusCreated,
},
{
Name: "ko, empty payload",
Tenant: ``,
HTTPStatus: http.StatusBadRequest,
},
{
Name: "ko, bad payload",
Tenant: `...`,
HTTPStatus: http.StatusBadRequest,
},
{
Name: "ko, empty tenant ID",
Tenant: `{"tenant_id": ""}`,
HTTPStatus: http.StatusBadRequest,
},
{
Name: "ko, error",
TenantID: "1234",
Tenant: `{"tenant_id": "1234"}`,
ProvisionTenantErr: errors.New("error"),
HTTPStatus: http.StatusInternalServerError,
},
}

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
deviceConnectApp := &app_mocks.App{}
if tc.TenantID != "" {
deviceConnectApp.On("ProvisionTenant",
mock.MatchedBy(func(_ context.Context) bool {
return true
}),
&model.Tenant{TenantID: tc.TenantID},
).Return(tc.ProvisionTenantErr)
}

router, _ := NewRouter(deviceConnectApp, nil)

req, err := http.NewRequest("POST", APIURLInternalTenants, strings.NewReader(tc.Tenant))
if !assert.NoError(t, err) {
t.FailNow()
}

w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, tc.HTTPStatus, w.Code)
if tc.HTTPStatus == http.StatusNoContent {
assert.Nil(t, w.Body.Bytes())
}

deviceConnectApp.AssertExpectations(t)
})
}
}

func TestInternalCheckUpdate(t *testing.T) {
testCases := []struct {
Name string
Expand Down
4 changes: 1 addition & 3 deletions api/http/router.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Northern.tech AS
// Copyright 2022 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,7 +35,6 @@ const (

APIURLInternalAlive = APIURLInternal + "/alive"
APIURLInternalHealth = APIURLInternal + "/health"
APIURLInternalTenants = APIURLInternal + "/tenants"
APIURLInternalDevices = APIURLInternal + "/tenants/:tenantId/devices"
APIURLInternalDevicesID = APIURLInternal +
"/tenants/:tenantId/devices/:deviceId"
Expand Down Expand Up @@ -75,7 +74,6 @@ func NewRouter(
router.GET(APIURLInternalHealth, status.Health)

internal := NewInternalController(app, natsClient)
router.POST(APIURLInternalTenants, internal.Provision)
router.POST(APIURLInternalDevicesIDCheckUpdate, internal.CheckUpdate)
router.POST(APIURLInternalDevicesIDSendInventory, internal.SendInventory)

Expand Down
8 changes: 1 addition & 7 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Northern.tech AS
// Copyright 2022 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,7 +39,6 @@ var (
//go:generate ../utils/mockgen.sh
type App interface {
HealthCheck(ctx context.Context) error
ProvisionTenant(ctx context.Context, tenant *model.Tenant) error
ProvisionDevice(ctx context.Context, tenantID string, device *model.Device) error
GetDevice(ctx context.Context, tenantID, deviceID string) (*model.Device, error)
DeleteDevice(ctx context.Context, tenantID, deviceID string) error
Expand Down Expand Up @@ -88,11 +87,6 @@ func (a *app) HealthCheck(ctx context.Context) error {
return a.store.Ping(ctx)
}

// ProvisionTenant provisions a new tenant
func (a *app) ProvisionTenant(ctx context.Context, tenant *model.Tenant) error {
return a.store.ProvisionTenant(ctx, tenant.TenantID)
}

// ProvisionDevice provisions a new tenant
func (a *app) ProvisionDevice(
ctx context.Context,
Expand Down
23 changes: 1 addition & 22 deletions app/app_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Northern.tech AS
// Copyright 2022 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -52,27 +52,6 @@ func TestHealthCheck(t *testing.T) {
store.AssertExpectations(t)
}

func TestProvisionTenant(t *testing.T) {
err := errors.New("error")
const tenantID = "1234"

store := &store_mocks.DataStore{}
store.On("ProvisionTenant",
mock.MatchedBy(func(ctx context.Context) bool {
return true
}),
tenantID,
).Return(err)

app := New(store, nil, nil)

ctx := context.Background()
res := app.ProvisionTenant(ctx, &model.Tenant{TenantID: tenantID})
assert.Equal(t, err, res)

store.AssertExpectations(t)
}

func TestProvisionDevice(t *testing.T) {
err := errors.New("error")
const tenantID = "1234"
Expand Down
16 changes: 1 addition & 15 deletions app/mocks/App.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 0 additions & 19 deletions docs/internal_api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,6 @@ paths:
schema:
$ref: '#/components/schemas/Error'

/tenants:
post:
tags:
- Internal API
operationId: Provision tenant
summary: Initialize internal state for a new tenant
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/NewTenant'
responses:
201:
description: Tenant initialized successfully.
400:
$ref: '#/components/responses/InvalidRequestError'
500:
$ref: '#/components/responses/InternalServerError'

/tenants/{tenantId}/devices:
post:
tags:
Expand Down
3 changes: 1 addition & 2 deletions store/datastore.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Northern.tech AS
// Copyright 2022 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,7 +27,6 @@ import (
//go:generate ../utils/mockgen.sh
type DataStore interface {
Ping(ctx context.Context) error
ProvisionTenant(ctx context.Context, tenantID string) error
ProvisionDevice(ctx context.Context, tenantID string, deviceID string) error
DeleteDevice(ctx context.Context, tenantID, deviceID string) error
GetDevice(ctx context.Context, tenantID, deviceID string) (*model.Device, error)
Expand Down
16 changes: 1 addition & 15 deletions store/mocks/DataStore.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions store/mongo/datastore_mongo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Northern.tech AS
// Copyright 2022 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -187,11 +187,6 @@ func (db *DataStoreMongo) Ping(ctx context.Context) error {
return res.Err()
}

// ProvisionTenant provisions a new tenant
func (db *DataStoreMongo) ProvisionTenant(ctx context.Context, tenantID string) error {
return Migrate(ctx, DbName, DbVersion, db.client, true)
}

// ProvisionDevice provisions a new device
func (db *DataStoreMongo) ProvisionDevice(ctx context.Context, tenantID, deviceID string) error {
coll := db.client.Database(DbName).Collection(DevicesCollectionName)
Expand Down
20 changes: 4 additions & 16 deletions store/mongo/datastore_mongo_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Northern.tech AS
// Copyright 2022 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -19,11 +19,12 @@ import (
"compress/gzip"
"context"
"encoding/base64"
"testing"
"time"

"github.com/mendersoftware/go-lib-micro/ws"
"github.com/mendersoftware/go-lib-micro/ws/shell"
"github.com/vmihailenco/msgpack/v5"
"testing"
"time"

"github.com/google/uuid"
"github.com/pkg/errors"
Expand Down Expand Up @@ -60,19 +61,6 @@ func TestPing(t *testing.T) {
assert.NoError(t, err)
}

func TestProvisionTenant(t *testing.T) {
if testing.Short() {
t.Skip("skipping TestPing in short mode.")
}
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*10)
defer cancel()

ds := DataStoreMongo{client: db.Client()}
defer ds.DropDatabase()
err := ds.ProvisionTenant(ctx, "1234")
assert.NoError(t, err)
}

func TestProvisionAndDeleteDevice(t *testing.T) {
if testing.Short() {
t.Skip("skipping TestPing in short mode.")
Expand Down

0 comments on commit eeb8699

Please sign in to comment.