Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MGMT-14933: Validate hosts running in OCI (#5413)
* MGMT-14933: Validate hosts running in OCI Check the manufacturer to check if hosts are running in OCI when platform OCI is selected. * update unit-tests and fix lint
- Loading branch information
1 parent
a30d935
commit 560aebc
Showing
8 changed files
with
176 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,20 @@ | ||
package external | ||
|
||
import ( | ||
"github.com/openshift/assisted-service/internal/provider" | ||
"github.com/openshift/assisted-service/models" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type externalProvider struct { | ||
Log logrus.FieldLogger | ||
platformType models.PlatformType | ||
// baseExternalProvider provides a default implementation suitable for platforms relying on the external platform. | ||
// Compose it and implement Name() to fullfil the Provider interface. | ||
type baseExternalProvider struct { | ||
Log logrus.FieldLogger | ||
} | ||
|
||
// NewExternalProvider creates a new none platform provider. | ||
func NewExternalProvider(log logrus.FieldLogger, platformType models.PlatformType) provider.Provider { | ||
return &externalProvider{ | ||
Log: log, | ||
platformType: platformType, | ||
} | ||
} | ||
|
||
// Name returns the name of the provider | ||
func (p *externalProvider) Name() models.PlatformType { | ||
return p.platformType | ||
} | ||
|
||
func (p *externalProvider) IsHostSupported(_ *models.Host) (bool, error) { | ||
func (p *baseExternalProvider) IsHostSupported(_ *models.Host) (bool, error) { | ||
return true, nil | ||
} | ||
|
||
func (p *externalProvider) AreHostsSupported(_ []*models.Host) (bool, error) { | ||
func (p *baseExternalProvider) AreHostsSupported(hosts []*models.Host) (bool, error) { | ||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package external | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/openshift/assisted-service/internal/common" | ||
"github.com/openshift/assisted-service/internal/provider" | ||
"github.com/openshift/assisted-service/models" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
const ( | ||
OCIManufacturer string = "OracleCloud.com" | ||
) | ||
|
||
type ociExternalProvider struct { | ||
baseExternalProvider | ||
} | ||
|
||
func NewOciExternalProvider(log logrus.FieldLogger) provider.Provider { | ||
return &ociExternalProvider{ | ||
baseExternalProvider: baseExternalProvider{ | ||
Log: log, | ||
}, | ||
} | ||
} | ||
|
||
func (p *baseExternalProvider) Name() models.PlatformType { | ||
return models.PlatformTypeOci | ||
} | ||
|
||
func (p *ociExternalProvider) IsHostSupported(host *models.Host) (bool, error) { | ||
// during the discovery there is a short time that host didn't return its inventory to the service | ||
if host.Inventory == "" { | ||
return false, nil | ||
} | ||
hostInventory, err := common.UnmarshalInventory(host.Inventory) | ||
if err != nil { | ||
return false, fmt.Errorf("error marshaling host to inventory, error %w", err) | ||
} | ||
return hostInventory.SystemVendor.Manufacturer == OCIManufacturer, nil | ||
} | ||
|
||
func (p *ociExternalProvider) AreHostsSupported(hosts []*models.Host) (bool, error) { | ||
for _, h := range hosts { | ||
supported, err := p.IsHostSupported(h) | ||
if err != nil { | ||
return false, fmt.Errorf("error while checking if host is supported, error is: %w", err) | ||
} | ||
if !supported { | ||
return false, nil | ||
} | ||
} | ||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package external | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/openshift/assisted-service/internal/common" | ||
"github.com/openshift/assisted-service/internal/provider" | ||
"github.com/openshift/assisted-service/models" | ||
) | ||
|
||
var _ = Describe("oci", func() { | ||
var log = common.GetTestLog() | ||
Context("host", func() { | ||
var provider provider.Provider | ||
var host *models.Host | ||
BeforeEach(func() { | ||
provider = NewOciExternalProvider(log) | ||
host = &models.Host{} | ||
}) | ||
|
||
setHostInventory := func(inventory *models.Inventory, host *models.Host) { | ||
data, err := json.Marshal(inventory) | ||
Expect(err).To(BeNil()) | ||
host.Inventory = string(data) | ||
} | ||
|
||
It("is supported", func() { | ||
inventory := &models.Inventory{ | ||
SystemVendor: &models.SystemVendor{ | ||
Manufacturer: OCIManufacturer, | ||
}, | ||
} | ||
setHostInventory(inventory, host) | ||
supported, err := provider.IsHostSupported(host) | ||
Expect(err).To(BeNil()) | ||
Expect(supported).To(BeTrue()) | ||
}) | ||
|
||
It("is not supported", func() { | ||
inventory := &models.Inventory{ | ||
SystemVendor: &models.SystemVendor{ | ||
Manufacturer: "", | ||
}, | ||
} | ||
setHostInventory(inventory, host) | ||
supported, err := provider.IsHostSupported(host) | ||
Expect(err).To(BeNil()) | ||
Expect(supported).To(BeFalse()) | ||
}) | ||
|
||
It("are supported", func() { | ||
inventory := &models.Inventory{ | ||
SystemVendor: &models.SystemVendor{ | ||
Manufacturer: OCIManufacturer, | ||
}, | ||
} | ||
setHostInventory(inventory, host) | ||
supported, err := provider.AreHostsSupported([]*models.Host{host, host}) | ||
Expect(err).To(BeNil()) | ||
Expect(supported).To(BeTrue()) | ||
}) | ||
|
||
It("are not supported", func() { | ||
inventory := &models.Inventory{ | ||
SystemVendor: &models.SystemVendor{ | ||
Manufacturer: OCIManufacturer, | ||
}, | ||
} | ||
setHostInventory(inventory, host) | ||
|
||
notOCIHost := &models.Host{} | ||
notOCIinventory := &models.Inventory{ | ||
SystemVendor: &models.SystemVendor{ | ||
Manufacturer: "", | ||
}, | ||
} | ||
setHostInventory(notOCIinventory, notOCIHost) | ||
|
||
supported, err := provider.AreHostsSupported([]*models.Host{host, notOCIHost}) | ||
Expect(err).To(BeNil()) | ||
Expect(supported).To(BeFalse()) | ||
}) | ||
|
||
It("has no inventory", func() { | ||
supported, err := provider.IsHostSupported(host) | ||
Expect(err).To(BeNil()) | ||
Expect(supported).To(BeFalse()) | ||
}) | ||
|
||
It("has an invalid inventory", func() { | ||
host.Inventory = "invalid-inventory" | ||
supported, err := provider.IsHostSupported(host) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(supported).To(BeFalse()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters