Skip to content

Commit

Permalink
feat: use dto in getversion
Browse files Browse the repository at this point in the history
  • Loading branch information
rjbrache committed Jul 22, 2024
1 parent 2d3bc4f commit 0a077aa
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 26 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ mock: ### run mockgen
mockgen -source ./internal/usecase/ciraconfigs/interfaces.go -package v1 -mock_names Repository=MockCIRAConfigsRepository,Feature=MockCIRAConfigsFeature > ./internal/controller/http/v1/ciraconfigs_mocks_test.go
mockgen -source ./internal/usecase/devices/interfaces.go -package devices_test > ./internal/usecase/devices/mocks_test.go
mockgen -source ./internal/usecase/devices/interfaces.go -package v1 -mock_names Repository=MockDeviceManagementRepository,Feature=MockDeviceManagementFeature > ./internal/controller/http/v1/devicemanagement_mocks_test.go
mockgen -source ./internal/usecase/devices/interfaces.go -package v2 -mock_names Repository=MockDeviceManagementRepository,Feature=MockDeviceManagementFeature > ./internal/controller/http/v2/devicemanagement_mocks_test.go
mockgen -source ./internal/usecase/domains/interfaces.go -package domains_test > ./internal/usecase/domains/mocks_test.go
mockgen -source ./internal/usecase/domains/interfaces.go -package v1 -mock_names Repository=MockDomainsRepository,Feature=MockDomainsFeature > ./internal/controller/http/v1/domains_mocks_test.go
mockgen -source ./internal/usecase/ieee8021xconfigs/interfaces.go -package ieee8021xconfigs_test > ./internal/usecase/ieee8021xconfigs/mocks_test.go
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/http/v1/devicemanagement_mocks_test.go

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

4 changes: 2 additions & 2 deletions internal/controller/http/v1/devicemanagement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ func TestDeviceManagement(t *testing.T) {
method: http.MethodGet,
mock: func(m *MockDeviceManagementFeature) {
m.EXPECT().GetVersion(context.Background(), "valid-guid").
Return(map[string]interface{}{"version": "1.0.0"}, nil)
Return(dto.Version{}, nil)
},
expectedCode: http.StatusOK,
response: map[string]interface{}{"version": "1.0.0"},
response: dto.Version{},
},
{
name: "getFeatures - successful retrieval",
Expand Down
16 changes: 16 additions & 0 deletions internal/controller/http/v2/devicemanagement.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,26 @@ func NewAmtRoutes(handler *gin.RouterGroup, d devices.Feature, l logger.Interfac

h := handler.Group("/amt")
{
h.GET("version/:guid", r.getVersion)

h.GET("features/:guid", r.getFeatures)
}
}

func (r *deviceManagementRoutes) getVersion(c *gin.Context) {
guid := c.Param("guid")

version, err := r.d.GetVersion(c.Request.Context(), guid)
if err != nil {
r.l.Error(err, "http - v2 - GetVersion")
v1.ErrorResponse(c, err)

return
}

c.JSON(http.StatusOK, version)
}

func (r *deviceManagementRoutes) getFeatures(c *gin.Context) {
guid := c.Param("guid")

Expand Down
6 changes: 3 additions & 3 deletions internal/controller/http/v2/devicemanagement_mocks_test.go

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

Binary file not shown.
11 changes: 11 additions & 0 deletions internal/controller/http/v2/devicemanagement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ func TestGetFeatures(t *testing.T) {
expectedCode int
response interface{}
}{
{
name: "getVersion - successful retrieval",
url: "/api/v2/amt/version/valid-guid",
method: http.MethodGet,
mock: func(m *MockDeviceManagementFeature) {
m.EXPECT().GetVersion(context.Background(), "valid-guid").
Return(dto.Version{}, nil)
},
expectedCode: http.StatusOK,
response: dto.Version{},
},
{
name: "getFeatures - successful retrieval",
url: "/api/v2/amt/features/valid-guid",
Expand Down
25 changes: 25 additions & 0 deletions internal/entity/dto/setupandconfiguration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dto

import (
"github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/wsman/amt/setupandconfiguration"
)

type (
SetupAndConfigurationServiceResponse struct {
RequestedState setupandconfiguration.RequestedState `xml:"RequestedState,omitempty"` // RequestedState is an integer enumeration that indicates the last requested or desired state for the element, irrespective of the mechanism through which it was requested.
EnabledState setupandconfiguration.EnabledState `xml:"EnabledState,omitempty"` // EnabledState is an integer enumeration that indicates the enabled and disabled states of an element.
ElementName string `xml:"ElementName,omitempty"` // A user-friendly name for the object. This property allows each instance to define a user-friendly name in addition to its key properties, identity data, and description information. Note that the Name property of ManagedSystemElement is also defined as a user-friendly name. But, it is often subclassed to be a Key. It is not reasonable that the same property can convey both identity and a user-friendly name, without inconsistencies. Where Name exists and is not a Key (such as for instances of LogicalDevice), the same information can be present in both the Name and ElementName properties. Note that if there is an associated instance of CIM_EnabledLogicalElementCapabilities, restrictions on this properties may exist as defined in ElementNameMask and MaxElementNameLen properties defined in that class.
SystemCreationClassName string `xml:"SystemCreationClassName,omitempty"` // The CreationClassName of the scoping System.
SystemName string `xml:"SystemName,omitempty"` // The Name of the scoping System.
CreationClassName string `xml:"CreationClassName,omitempty"` // CreationClassName indicates the name of the class or the subclass that is used in the creation of an instance. When used with the other key properties of this class, this property allows all instances of this class and its subclasses to be uniquely identified.
Name string `xml:"Name,omitempty"` // The Name property uniquely identifies the Service and provides an indication of the functionality that is managed. This functionality is described in more detail in the Description property of the object.
ProvisioningMode setupandconfiguration.ProvisioningModeValue `xml:"ProvisioningMode,omitempty"` // A Read-Only enumeration value that determines the behavior of Intel® AMT when it is deployed.
ProvisioningState setupandconfiguration.ProvisioningStateValue `xml:"ProvisioningState,omitempty"` // An enumeration value that indicates the state of the Intel® AMT subsystem in the provisioning process"Pre" - the setup operation has not started."In" - the setup operation is in progress."Post" - Intel® AMT is configured.
ZeroTouchConfigurationEnabled bool `xml:"ZeroTouchConfigurationEnabled,omitempty"` // Indicates if Zero Touch Configuration (Remote Configuration) is enabled or disabled. This property affects only enterprise mode. It can be modified while in SMB mode
ProvisioningServerOTP string `xml:"ProvisioningServerOTP,omitempty"` // A optional binary data value containing 8-32 characters,that represents a one-time password (OTP), used to authenticate the Intel® AMT to the configuration server. This property can be retrieved only in IN Provisioning state, nevertheless, it is settable also in POST provisioning state.
ConfigurationServerFQDN string `xml:"ConfigurationServerFQDN,omitempty"` // The FQDN of the configuration server.
PasswordModel setupandconfiguration.PasswordModelValue `xml:"PasswordModel,omitempty"` // An enumeration value that determines the password model of Intel® AMT.
DhcpDNSSuffix string `xml:"DhcpDNSSuffix,omitempty"` // Domain name received from DHCP
TrustedDNSSuffix string `xml:"TrustedDNSSuffix,omitempty"` // Trusted domain name configured in MEBX
}
)
14 changes: 14 additions & 0 deletions internal/entity/dto/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dto

type (
SoftwareIdentity struct {
InstanceID string `json:"instanceID"`
VersionString string `json:"versionString" example:"<major>.<minor>.<revision>.<build>"`
IsEntity bool `json:"isEntity" example:"true"`
}

Version struct {
CIMSoftwareIdentity []SoftwareIdentity `json:"cimSoftwareIdentity"`
AMTSetupAndConfigurationService SetupAndConfigurationServiceResponse `json:"amtSetupAndConfigurationService"`
}
)
71 changes: 59 additions & 12 deletions internal/usecase/devices/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,49 @@ package devices
import (
"context"

"github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/wsman/amt/setupandconfiguration"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/wsman/cim/software"

"github.com/open-amt-cloud-toolkit/console/internal/entity/dto"
)

func (uc *UseCase) GetVersion(c context.Context, guid string) (map[string]interface{}, error) {
func (uc *UseCase) GetVersion(c context.Context, guid string) (dto.Version, error) {
item, err := uc.GetByID(c, guid, "")
if err != nil {
return nil, err
return dto.Version{}, err
}

uc.device.SetupWsmanClient(*item, false, true)

version, err := uc.device.GetAMTVersion()
softwareIdentity, err := uc.device.GetAMTVersion()
if err != nil {
return nil, err
return dto.Version{}, err
}

data, err := uc.device.GetSetupAndConfiguration()
if err != nil {
return nil, err
return dto.Version{}, err
}

response := map[string]interface{}{
"CIM_SoftwareIdentity": map[string]interface{}{
"responses": version,
},
"AMT_SetupAndConfigurationService": map[string]interface{}{
"response": data[0],
},
// iterate over the data and convert each entity to dto
d1 := make([]dto.SoftwareIdentity, len(softwareIdentity))

for i := range softwareIdentity {
tmpEntity := softwareIdentity[i] // create a new variable to avoid memory aliasing
d1[i] = *uc.softwareIdentityEntityToDTO(&tmpEntity)
}

// iterate over the data and convert each entity to dto
d2 := make([]dto.SetupAndConfigurationServiceResponse, len(data))

for i := range data {
tmpEntity := data[i] // create a new variable to avoid memory aliasing
d2[i] = *uc.setupAndConfigurationServiceResponseEntityToDTO(&tmpEntity)
}

response := dto.Version{
CIMSoftwareIdentity: d1,
AMTSetupAndConfigurationService: d2[0],
}

return response, nil
Expand Down Expand Up @@ -163,3 +178,35 @@ func (uc *UseCase) GetGeneralSettings(c context.Context, guid string) (interface

return response, nil
}

func (uc *UseCase) softwareIdentityEntityToDTO(d *software.SoftwareIdentity) *dto.SoftwareIdentity {
d1 := &dto.SoftwareIdentity{
InstanceID: d.InstanceID,
VersionString: d.VersionString,
IsEntity: d.IsEntity,
}

return d1
}

func (uc *UseCase) setupAndConfigurationServiceResponseEntityToDTO(d *setupandconfiguration.SetupAndConfigurationServiceResponse) *dto.SetupAndConfigurationServiceResponse {
d1 := &dto.SetupAndConfigurationServiceResponse{
RequestedState: d.RequestedState,
EnabledState: d.EnabledState,
ElementName: d.ElementName,
SystemCreationClassName: d.SystemCreationClassName,
SystemName: d.SystemName,
CreationClassName: d.CreationClassName,
Name: d.Name,
ProvisioningMode: d.ProvisioningMode,
ProvisioningState: d.ProvisioningState,
ZeroTouchConfigurationEnabled: d.ZeroTouchConfigurationEnabled,
ProvisioningServerOTP: d.ProvisioningServerOTP,
ConfigurationServerFQDN: d.ConfigurationServerFQDN,
PasswordModel: d.PasswordModel,
DhcpDNSSuffix: d.DhcpDNSSuffix,
TrustedDNSSuffix: d.TrustedDNSSuffix,
}

return d1
}
8 changes: 4 additions & 4 deletions internal/usecase/devices/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestGetVersion(t *testing.T) {
Return(device, nil)
},

res: map[string]interface{}{"AMT_SetupAndConfigurationService": map[string]interface{}{"response": setupandconfiguration.SetupAndConfigurationServiceResponse{XMLName: xml.Name{Space: "", Local: "AMT_SetupAndConfigurationService"}, RequestedState: 1, EnabledState: 1, ElementName: "SampleElementName", SystemCreationClassName: "SampleSystemCreationClassName", SystemName: "SampleSystemName", CreationClassName: "SampleCreationClassName", Name: "SampleName", ProvisioningMode: 1, ProvisioningState: 1, ZeroTouchConfigurationEnabled: true, ProvisioningServerOTP: "SampleProvisioningServerOTP", ConfigurationServerFQDN: "SampleConfigurationServerFQDN", PasswordModel: 1, DhcpDNSSuffix: "SampleDhcpDNSSuffix", TrustedDNSSuffix: "SampleTrustedDNSSuffix"}}, "CIM_SoftwareIdentity": map[string]interface{}{"responses": []software.SoftwareIdentity{}}},
res: dto.Version{CIMSoftwareIdentity: []dto.SoftwareIdentity{}, AMTSetupAndConfigurationService: dto.SetupAndConfigurationServiceResponse{RequestedState: 1, EnabledState: 1, ElementName: "SampleElementName", SystemCreationClassName: "SampleSystemCreationClassName", SystemName: "SampleSystemName", CreationClassName: "SampleCreationClassName", Name: "SampleName", ProvisioningMode: 1, ProvisioningState: 1, ZeroTouchConfigurationEnabled: true, ProvisioningServerOTP: "SampleProvisioningServerOTP", ConfigurationServerFQDN: "SampleConfigurationServerFQDN", PasswordModel: 1, DhcpDNSSuffix: "SampleDhcpDNSSuffix", TrustedDNSSuffix: "SampleTrustedDNSSuffix"}},

err: nil,
},
Expand All @@ -131,7 +131,7 @@ func TestGetVersion(t *testing.T) {
Return(nil, ErrGeneral)
},

res: map[string]interface{}(nil),
res: dto.Version{CIMSoftwareIdentity: []dto.SoftwareIdentity(nil), AMTSetupAndConfigurationService: dto.SetupAndConfigurationServiceResponse{RequestedState: 0, EnabledState: 0, ElementName: "", SystemCreationClassName: "", SystemName: "", CreationClassName: "", Name: "", ProvisioningMode: 0, ProvisioningState: 0, ZeroTouchConfigurationEnabled: false, ProvisioningServerOTP: "", ConfigurationServerFQDN: "", PasswordModel: 0, DhcpDNSSuffix: "", TrustedDNSSuffix: ""}},

err: devices.ErrDatabase,
},
Expand All @@ -157,7 +157,7 @@ func TestGetVersion(t *testing.T) {
Return(device, nil)
},

res: map[string]interface{}(nil),
res: dto.Version{},

err: ErrGeneral,
},
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestGetVersion(t *testing.T) {
Return(device, nil)
},

res: map[string]interface{}(nil),
res: dto.Version{CIMSoftwareIdentity: []dto.SoftwareIdentity(nil), AMTSetupAndConfigurationService: dto.SetupAndConfigurationServiceResponse{RequestedState: 0, EnabledState: 0, ElementName: "", SystemCreationClassName: "", SystemName: "", CreationClassName: "", Name: "", ProvisioningMode: 0, ProvisioningState: 0, ZeroTouchConfigurationEnabled: false, ProvisioningServerOTP: "", ConfigurationServerFQDN: "", PasswordModel: 0, DhcpDNSSuffix: "", TrustedDNSSuffix: ""}},

err: ErrGeneral,
},
Expand Down
2 changes: 1 addition & 1 deletion internal/usecase/devices/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ type (
Insert(ctx context.Context, d *dto.Device) (*dto.Device, error)
GetByColumn(ctx context.Context, columnName, queryValue, tenantID string) ([]dto.Device, error)
// Management Calls
GetVersion(ctx context.Context, guid string) (map[string]interface{}, error)
GetVersion(ctx context.Context, guid string) (dto.Version, error)
GetFeatures(ctx context.Context, guid string) (dto.Features, error)
SetFeatures(ctx context.Context, guid string, features dto.Features) (dto.Features, error)
GetAlarmOccurrences(ctx context.Context, guid string) ([]dto.AlarmClockOccurrence, error)
Expand Down
4 changes: 2 additions & 2 deletions internal/usecase/devices/mocks_test.go

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

0 comments on commit 0a077aa

Please sign in to comment.