Skip to content

Commit

Permalink
refactor(yurt-iot-dock): merge v2 and v3 client to deal with code dup…
Browse files Browse the repository at this point in the history
…lication

Signed-off-by: LavenderQAQ <[email protected]>
  • Loading branch information
LavenderQAQ committed Sep 1, 2023
1 parent 16e3a34 commit 3ecb55c
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 1,675 deletions.
4 changes: 4 additions & 0 deletions cmd/yurt-iot-dock/app/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ func Run(opts *options.YurtIoTDockOptions, stopCh <-chan struct{}) {
}

edgexdock := edgexclients.NewEdgexDock(opts.Version, opts.CoreMetadataAddr, opts.CoreCommandAddr)
if edgexdock == nil {
setupLog.Error(err, "failed to launch yurt-iot-dock, this maybe because the yurt-iot-dock is not compatible with the current edgex version")
os.Exit(1)
}

// setup the DeviceProfile Reconciler and Syncer
if err = (&controllers.DeviceProfileReconciler{
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/aliyun/alibaba-cloud-sdk-go v1.62.156
github.com/davecgh/go-spew v1.1.1
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.3.0
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0
github.com/go-resty/resty/v2 v2.7.0
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/google/uuid v1.3.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.3.0 h1:8Svk1HTehXEgwxgyA4muVhSkP3D9n1q+oSHI3B1Ac90=
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.3.0/go.mod h1:4/e61acxVkhQWCTjQ4XcHVJDnrMDloFsZZB1B6STCRw=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0 h1:xjwCI34DLM31cSl1q9XmYgXS3JqXufQJMgohnLLLDx0=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0/go.mod h1:zzzWGWij6wAqm1go9TLs++TFMIsBqBb1eRnIj4mRxGw=
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153 h1:yUdfgN0XgIJw7foRItutHYUIhlcKzcSf5vDpdhQAKTc=
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v2
package client

import (
"context"
Expand All @@ -39,33 +39,39 @@ import (

type EdgexDeviceClient struct {
*resty.Client
CoreMetaAddr string
CoreCommandAddr string
CoreMetaAddr string
CoreCommandAddr string
APIVersion string
DevicePath string
CommandResponsePath string
}

func NewEdgexDeviceClient(coreMetaAddr, coreCommandAddr string) *EdgexDeviceClient {
func NewEdgexDeviceClient(coreMetaAddr, coreCommandAddr, APIVersion string) *EdgexDeviceClient {
cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
instance := resty.NewWithClient(&http.Client{
Jar: cookieJar,
Timeout: 10 * time.Second,
})
return &EdgexDeviceClient{
Client: instance,
CoreMetaAddr: coreMetaAddr,
CoreCommandAddr: coreCommandAddr,
Client: instance,
CoreMetaAddr: coreMetaAddr,
CoreCommandAddr: coreCommandAddr,
APIVersion: APIVersion,
DevicePath: fmt.Sprintf(DevicePathTemplate, APIVersion),
CommandResponsePath: fmt.Sprintf(CommandResponsePathTemplate, APIVersion),
}
}

// Create function sends a POST request to EdgeX to add a new device
func (efc *EdgexDeviceClient) Create(ctx context.Context, device *iotv1alpha1.Device, options clients.CreateOptions) (*iotv1alpha1.Device, error) {
devs := []*iotv1alpha1.Device{device}
req := makeEdgeXDeviceRequest(devs)
req := makeEdgeXDeviceRequest(devs, efc.APIVersion)
klog.V(5).Infof("will add the Device: %s", device.Name)
reqBody, err := json.Marshal(req)
if err != nil {
return nil, err
}
postPath := fmt.Sprintf("http://%s%s", efc.CoreMetaAddr, DevicePath)
postPath := fmt.Sprintf("http://%s%s", efc.CoreMetaAddr, efc.DevicePath)
resp, err := efc.R().
SetBody(reqBody).Post(postPath)
if err != nil {
Expand Down Expand Up @@ -95,7 +101,7 @@ func (efc *EdgexDeviceClient) Create(ctx context.Context, device *iotv1alpha1.De
// Delete function sends a request to EdgeX to delete a device
func (efc *EdgexDeviceClient) Delete(ctx context.Context, name string, options clients.DeleteOptions) error {
klog.V(5).Infof("will delete the Device: %s", name)
delURL := fmt.Sprintf("http://%s%s/name/%s", efc.CoreMetaAddr, DevicePath, name)
delURL := fmt.Sprintf("http://%s%s/name/%s", efc.CoreMetaAddr, efc.DevicePath, name)
resp, err := efc.R().Delete(delURL)
if err != nil {
return err
Expand All @@ -110,12 +116,12 @@ func (efc *EdgexDeviceClient) Delete(ctx context.Context, name string, options c
// TODO support to update other fields
func (efc *EdgexDeviceClient) Update(ctx context.Context, device *iotv1alpha1.Device, options clients.UpdateOptions) (*iotv1alpha1.Device, error) {
actualDeviceName := getEdgeXName(device)
patchURL := fmt.Sprintf("http://%s%s", efc.CoreMetaAddr, DevicePath)
patchURL := fmt.Sprintf("http://%s%s", efc.CoreMetaAddr, efc.DevicePath)
if device == nil {
return nil, nil
}
devs := []*iotv1alpha1.Device{device}
req := makeEdgeXDeviceUpdateRequest(devs)
req := makeEdgeXDeviceUpdateRequest(devs, efc.APIVersion)
reqBody, err := json.Marshal(req)
if err != nil {
return nil, err
Expand All @@ -136,7 +142,7 @@ func (efc *EdgexDeviceClient) Update(ctx context.Context, device *iotv1alpha1.De
func (efc *EdgexDeviceClient) Get(ctx context.Context, deviceName string, options clients.GetOptions) (*iotv1alpha1.Device, error) {
klog.V(5).Infof("will get Devices: %s", deviceName)
var dResp edgex_resp.DeviceResponse
getURL := fmt.Sprintf("http://%s%s/name/%s", efc.CoreMetaAddr, DevicePath, deviceName)
getURL := fmt.Sprintf("http://%s%s/name/%s", efc.CoreMetaAddr, efc.DevicePath, deviceName)
resp, err := efc.R().Get(getURL)
if err != nil {
return nil, err
Expand All @@ -155,7 +161,7 @@ func (efc *EdgexDeviceClient) Get(ctx context.Context, deviceName string, option
// List is used to get all device objects on edge platform
// TODO:support label filtering according to options
func (efc *EdgexDeviceClient) List(ctx context.Context, options clients.ListOptions) ([]iotv1alpha1.Device, error) {
lp := fmt.Sprintf("http://%s%s/all?limit=-1", efc.CoreMetaAddr, DevicePath)
lp := fmt.Sprintf("http://%s%s/all?limit=-1", efc.CoreMetaAddr, efc.DevicePath)
resp, err := efc.R().EnableTrace().Get(lp)
if err != nil {
return nil, err
Expand Down Expand Up @@ -354,7 +360,7 @@ func (efc *EdgexDeviceClient) GetCommandResponseByName(deviceName string) ([]dto
klog.V(5).Infof("will get CommandResponses of device: %s", deviceName)

var dcr edgex_resp.DeviceCoreCommandResponse
getURL := fmt.Sprintf("http://%s%s/name/%s", efc.CoreCommandAddr, CommandResponsePath, deviceName)
getURL := fmt.Sprintf("http://%s%s/name/%s", efc.CoreCommandAddr, efc.CommandResponsePath, deviceName)

resp, err := efc.R().Get(getURL)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v2
package client

import (
"context"
Expand Down Expand Up @@ -46,7 +46,7 @@ const (
DeviceUpdateProperty = `{"apiVersion":"v2","statusCode":200}`
)

var deviceClient = NewEdgexDeviceClient("edgex-core-metadata:59881", "edgex-core-command:59882")
var deviceClient = NewEdgexDeviceClient("edgex-core-metadata:59881", "edgex-core-command:59882", "v2")

func Test_Get(t *testing.T) {
httpmock.ActivateNonDefault(deviceClient.Client.GetClient())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v2
package client

import (
"context"
Expand All @@ -33,25 +33,31 @@ import (

type EdgexDeviceProfile struct {
*resty.Client
CoreMetaAddr string
CoreMetaAddr string
APIVersion string
DeviceProfilePath string
CommandResponsePath string
}

func NewEdgexDeviceProfile(coreMetaAddr string) *EdgexDeviceProfile {
func NewEdgexDeviceProfile(coreMetaAddr, APIVersion string) *EdgexDeviceProfile {
return &EdgexDeviceProfile{
Client: resty.New(),
CoreMetaAddr: coreMetaAddr,
Client: resty.New(),
CoreMetaAddr: coreMetaAddr,
APIVersion: APIVersion,
DeviceProfilePath: fmt.Sprintf(DeviceProfilePathTemplate, APIVersion),
CommandResponsePath: fmt.Sprintf(CommandResponsePathTemplate, APIVersion),
}
}

// TODO: support label filtering
func getListDeviceProfileURL(address string, opts devcli.ListOptions) (string, error) {
url := fmt.Sprintf("http://%s%s/all?limit=-1", address, DeviceProfilePath)
func (cdc *EdgexDeviceProfile) getListDeviceProfileURL(address string, opts devcli.ListOptions) (string, error) {
url := fmt.Sprintf("http://%s%s/all?limit=-1", address, cdc.DeviceProfilePath)
return url, nil
}

func (cdc *EdgexDeviceProfile) List(ctx context.Context, opts devcli.ListOptions) ([]v1alpha1.DeviceProfile, error) {
klog.V(5).Info("will list DeviceProfiles")
lp, err := getListDeviceProfileURL(cdc.CoreMetaAddr, opts)
lp, err := cdc.getListDeviceProfileURL(cdc.CoreMetaAddr, opts)
if err != nil {
return nil, err
}
Expand All @@ -73,7 +79,7 @@ func (cdc *EdgexDeviceProfile) List(ctx context.Context, opts devcli.ListOptions
func (cdc *EdgexDeviceProfile) Get(ctx context.Context, name string, opts devcli.GetOptions) (*v1alpha1.DeviceProfile, error) {
klog.V(5).Infof("will get DeviceProfiles: %s", name)
var dpResp responses.DeviceProfileResponse
getURL := fmt.Sprintf("http://%s%s/name/%s", cdc.CoreMetaAddr, DeviceProfilePath, name)
getURL := fmt.Sprintf("http://%s%s/name/%s", cdc.CoreMetaAddr, cdc.DeviceProfilePath, name)
resp, err := cdc.R().Get(getURL)
if err != nil {
return nil, err
Expand All @@ -90,13 +96,13 @@ func (cdc *EdgexDeviceProfile) Get(ctx context.Context, name string, opts devcli

func (cdc *EdgexDeviceProfile) Create(ctx context.Context, deviceProfile *v1alpha1.DeviceProfile, opts devcli.CreateOptions) (*v1alpha1.DeviceProfile, error) {
dps := []*v1alpha1.DeviceProfile{deviceProfile}
req := makeEdgeXDeviceProfilesRequest(dps)
req := makeEdgeXDeviceProfilesRequest(dps, cdc.APIVersion)
klog.V(5).Infof("will add the DeviceProfile: %s", deviceProfile.Name)
reqBody, err := json.Marshal(req)
if err != nil {
return nil, err
}
postURL := fmt.Sprintf("http://%s%s", cdc.CoreMetaAddr, DeviceProfilePath)
postURL := fmt.Sprintf("http://%s%s", cdc.CoreMetaAddr, cdc.DeviceProfilePath)
resp, err := cdc.R().SetBody(reqBody).Post(postURL)
if err != nil {
return nil, err
Expand Down Expand Up @@ -129,7 +135,7 @@ func (cdc *EdgexDeviceProfile) Update(ctx context.Context, deviceProfile *v1alph

func (cdc *EdgexDeviceProfile) Delete(ctx context.Context, name string, opts devcli.DeleteOptions) error {
klog.V(5).Infof("will delete the DeviceProfile: %s", name)
delURL := fmt.Sprintf("http://%s%s/name/%s", cdc.CoreMetaAddr, DeviceProfilePath, name)
delURL := fmt.Sprintf("http://%s%s/name/%s", cdc.CoreMetaAddr, cdc.DeviceProfilePath, name)
resp, err := cdc.R().Delete(delURL)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v2
package client

import (
"context"
Expand All @@ -38,7 +38,7 @@ const (
ProfileDeleteFail = `{"apiVersion":"v2","message":"fail to delete the device profile with name test-Random-Boolean-Device","statusCode":404}`
)

var profileClient = NewEdgexDeviceProfile("edgex-core-metadata:59881")
var profileClient = NewEdgexDeviceProfile("edgex-core-metadata:59881", "v2")

func Test_ListProfile(t *testing.T) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v2
package client

import (
"context"
Expand All @@ -33,26 +33,32 @@ import (

type EdgexDeviceServiceClient struct {
*resty.Client
CoreMetaAddr string
CoreMetaAddr string
APIVersion string
DeviceServicePath string
CommandResponsePath string
}

func NewEdgexDeviceServiceClient(coreMetaAddr string) *EdgexDeviceServiceClient {
func NewEdgexDeviceServiceClient(coreMetaAddr, APIVersion string) *EdgexDeviceServiceClient {
return &EdgexDeviceServiceClient{
Client: resty.New(),
CoreMetaAddr: coreMetaAddr,
Client: resty.New(),
CoreMetaAddr: coreMetaAddr,
APIVersion: APIVersion,
DeviceServicePath: fmt.Sprintf(DeviceServicePathTemplate, APIVersion),
CommandResponsePath: fmt.Sprintf(CommandResponsePathTemplate, APIVersion),
}
}

// Create function sends a POST request to EdgeX to add a new deviceService
func (eds *EdgexDeviceServiceClient) Create(ctx context.Context, deviceService *v1alpha1.DeviceService, options edgeCli.CreateOptions) (*v1alpha1.DeviceService, error) {
dss := []*v1alpha1.DeviceService{deviceService}
req := makeEdgeXDeviceService(dss)
req := makeEdgeXDeviceService(dss, eds.APIVersion)
klog.V(5).InfoS("will add the DeviceServices", "DeviceService", deviceService.Name)
jsonBody, err := json.Marshal(req)
if err != nil {
return nil, err
}
postPath := fmt.Sprintf("http://%s%s", eds.CoreMetaAddr, DeviceServicePath)
postPath := fmt.Sprintf("http://%s%s", eds.CoreMetaAddr, eds.DeviceServicePath)
resp, err := eds.R().
SetBody(jsonBody).Post(postPath)
if err != nil {
Expand Down Expand Up @@ -82,7 +88,7 @@ func (eds *EdgexDeviceServiceClient) Create(ctx context.Context, deviceService *
// Delete function sends a request to EdgeX to delete a deviceService
func (eds *EdgexDeviceServiceClient) Delete(ctx context.Context, name string, option edgeCli.DeleteOptions) error {
klog.V(5).InfoS("will delete the DeviceService", "DeviceService", name)
delURL := fmt.Sprintf("http://%s%s/name/%s", eds.CoreMetaAddr, DeviceServicePath, name)
delURL := fmt.Sprintf("http://%s%s/name/%s", eds.CoreMetaAddr, eds.DeviceServicePath, name)
resp, err := eds.R().Delete(delURL)
if err != nil {
return err
Expand All @@ -96,7 +102,7 @@ func (eds *EdgexDeviceServiceClient) Delete(ctx context.Context, name string, op
// Update is used to set the admin or operating state of the deviceService by unique name of the deviceService.
// TODO support to update other fields
func (eds *EdgexDeviceServiceClient) Update(ctx context.Context, ds *v1alpha1.DeviceService, options edgeCli.UpdateOptions) (*v1alpha1.DeviceService, error) {
patchURL := fmt.Sprintf("http://%s%s", eds.CoreMetaAddr, DeviceServicePath)
patchURL := fmt.Sprintf("http://%s%s", eds.CoreMetaAddr, eds.DeviceServicePath)
if ds == nil {
return nil, nil
}
Expand Down Expand Up @@ -127,7 +133,7 @@ func (eds *EdgexDeviceServiceClient) Update(ctx context.Context, ds *v1alpha1.De
func (eds *EdgexDeviceServiceClient) Get(ctx context.Context, name string, options edgeCli.GetOptions) (*v1alpha1.DeviceService, error) {
klog.V(5).InfoS("will get DeviceServices", "DeviceService", name)
var dsResp responses.DeviceServiceResponse
getURL := fmt.Sprintf("http://%s%s/name/%s", eds.CoreMetaAddr, DeviceServicePath, name)
getURL := fmt.Sprintf("http://%s%s/name/%s", eds.CoreMetaAddr, eds.DeviceServicePath, name)
resp, err := eds.R().Get(getURL)
if err != nil {
return nil, err
Expand All @@ -147,7 +153,7 @@ func (eds *EdgexDeviceServiceClient) Get(ctx context.Context, name string, optio
// The Hanoi version currently supports only a single label and does not support other filters
func (eds *EdgexDeviceServiceClient) List(ctx context.Context, options edgeCli.ListOptions) ([]v1alpha1.DeviceService, error) {
klog.V(5).Info("will list DeviceServices")
lp := fmt.Sprintf("http://%s%s/all?limit=-1", eds.CoreMetaAddr, DeviceServicePath)
lp := fmt.Sprintf("http://%s%s/all?limit=-1", eds.CoreMetaAddr, eds.DeviceServicePath)
resp, err := eds.R().
EnableTrace().
Get(lp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v2
package client

import (
"context"
Expand All @@ -40,7 +40,7 @@ const (
ServiceUpdateFail = `[{"apiVersion":"v2","message":"fail to query object *models.DeviceService, because id: md|ds:01dfe04d-f361-41fd-b1c4-7ca0718f461a doesn't exist in the database","statusCode":404}]`
)

var serviceClient = NewEdgexDeviceServiceClient("edgex-core-metadata:59881")
var serviceClient = NewEdgexDeviceServiceClient("edgex-core-metadata:59881", "v2")

func Test_GetService(t *testing.T) {
httpmock.ActivateNonDefault(serviceClient.Client.GetClient())
Expand Down
Loading

0 comments on commit 3ecb55c

Please sign in to comment.