airshipctl/pkg/inventory/inventory.go
siraj.yasin f45ece8294 Update metadata.yaml to kubernetes style
* Define GVK for metadata.yaml
* Added supoorting type and registered the kind 'AirshipMetadata' to
  runtime scheme
* Used document object to read metadata instead of reading it as config

Relates-To: #530

Change-Id: I748cd0921ba7bb04ca6fb2999294776e6803ed3e
2021-06-04 03:34:31 +00:00

82 lines
2.0 KiB
Go

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
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 inventory
import (
"path/filepath"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/document/metadata"
"opendev.org/airship/airshipctl/pkg/inventory/baremetal"
"opendev.org/airship/airshipctl/pkg/inventory/ifc"
)
var _ ifc.Inventory = Inventory{}
// Inventory implementation of the interface
type Inventory struct {
config.Factory
}
// NewInventory inventory constructor
func NewInventory(f config.Factory) ifc.Inventory {
return Inventory{
Factory: f,
}
}
// BaremetalInventory implementation of the interface
func (i Inventory) BaremetalInventory() (ifc.BaremetalInventory, error) {
cfg, err := i.Factory()
if err != nil {
return nil, err
}
mgmCfg, err := cfg.CurrentContextManagementConfig()
if err != nil {
return nil, err
}
targetPath, err := cfg.CurrentContextTargetPath()
if err != nil {
return nil, err
}
phaseDir, err := cfg.CurrentContextInventoryRepositoryName()
if err != nil {
return nil, err
}
metadataPath, err := cfg.CurrentContextMetadataPath()
if err != nil {
return nil, err
}
metadataBundle := filepath.Join(targetPath, phaseDir, metadataPath)
meta, err := metadata.Config(metadataBundle)
if err != nil {
return nil, err
}
inventoryBundle := filepath.Join(targetPath, phaseDir, meta.InventoryPath)
bundle, err := document.NewBundleByPath(inventoryBundle)
if err != nil {
return nil, err
}
return baremetal.NewInventory(mgmCfg, bundle), nil
}