Add InventoryRepoName field to config

If inventory repo name is not specified, phaseRepoDir will be used

Change-Id: Iea3eca5d7ca14c549696b7214c8d4b07ef3e5bf4
This commit is contained in:
Kostiantyn Kalynovskyi 2021-01-22 22:26:37 +00:00
parent 129fd7ffa6
commit fe8c86feb8
12 changed files with 75 additions and 12 deletions

View File

@ -1,11 +1,14 @@
inventoryRepositoryName: ""
metadataPath: "" metadataPath: ""
phaseRepositoryName: bar_phase_repo phaseRepositoryName: bar_phase_repo
targetPath: bar_target_path targetPath: bar_target_path
inventoryRepositoryName: ""
metadataPath: "" metadataPath: ""
phaseRepositoryName: baz_phase_repo phaseRepositoryName: baz_phase_repo
targetPath: baz_target_path targetPath: baz_target_path
inventoryRepositoryName: ""
metadataPath: "" metadataPath: ""
phaseRepositoryName: foo_phase_repo phaseRepositoryName: foo_phase_repo
targetPath: foo_target_path targetPath: foo_target_path

View File

@ -1,3 +1,4 @@
inventoryRepositoryName: ""
metadataPath: "" metadataPath: ""
phaseRepositoryName: foo_phase_repo phaseRepositoryName: foo_phase_repo
targetPath: foo_target_path targetPath: foo_target_path

View File

@ -348,7 +348,25 @@ func (c *Config) CurrentContextPhaseRepositoryDir() (string, error) {
} }
repo, exist := ccm.Repositories[ccm.PhaseRepositoryName] repo, exist := ccm.Repositories[ccm.PhaseRepositoryName]
if !exist { if !exist {
return "", ErrMissingRepositoryName{} return "", ErrMissingRepositoryName{RepoType: "phase"}
}
return util.GitDirNameFromURL(repo.URL()), nil
}
// CurrentContextInventoryRepositoryName returns phase inventory directory from current context's manifest
// if it is not defined PhaseRepositoryName will be used instead
func (c *Config) CurrentContextInventoryRepositoryName() (string, error) {
ccm, err := c.CurrentContextManifest()
if err != nil {
return "", err
}
repoName := ccm.InventoryRepositoryName
if repoName == "" {
repoName = ccm.PhaseRepositoryName
}
repo, exist := ccm.Repositories[repoName]
if !exist {
return "", ErrMissingRepositoryName{RepoType: "inventory"}
} }
return util.GitDirNameFromURL(repo.URL()), nil return util.GitDirNameFromURL(repo.URL()), nil
} }

View File

@ -336,10 +336,40 @@ func TestCurrentPhaseRepositoryDir(t *testing.T) {
conf.Manifests[defaultString].PhaseRepositoryName = "nonexisting" conf.Manifests[defaultString].PhaseRepositoryName = "nonexisting"
phaseRepoDir, err = conf.CurrentContextPhaseRepositoryDir() phaseRepoDir, err = conf.CurrentContextPhaseRepositoryDir()
require.Error(t, err) require.Error(t, err)
assert.Equal(t, config.ErrMissingRepositoryName{}, err) assert.Equal(t, config.ErrMissingRepositoryName{RepoType: "phase"}, err)
assert.Equal(t, "", phaseRepoDir) assert.Equal(t, "", phaseRepoDir)
} }
func TestCurrentInventoryRepositoryDir(t *testing.T) {
conf, cleanup := testutil.InitConfig(t)
defer cleanup(t)
conf.CurrentContext = currentContextName
conf.Contexts[currentContextName].Manifest = defaultString
invRepoDir, err := conf.CurrentContextInventoryRepositoryName()
require.NoError(t, err)
assert.Equal(t, util.GitDirNameFromURL(
conf.Manifests[defaultString].Repositories[conf.Manifests[defaultString].PhaseRepositoryName].URL()),
invRepoDir)
conf.Manifests[defaultString].InventoryRepositoryName = "nonexisting"
invRepoDir, err = conf.CurrentContextInventoryRepositoryName()
require.Error(t, err)
assert.Equal(t, config.ErrMissingRepositoryName{RepoType: "inventory"}, err)
assert.Equal(t, "", invRepoDir)
invRepoName := "inv-repo"
invRepoURL := "/my-repository"
conf.Manifests[defaultString].Repositories[invRepoName] = &config.Repository{URLString: invRepoURL}
conf.Manifests[defaultString].InventoryRepositoryName = invRepoName
invRepoDir, err = conf.CurrentContextInventoryRepositoryName()
require.NoError(t, err)
assert.Equal(t, util.GitDirNameFromURL(
conf.Manifests[defaultString].Repositories[conf.Manifests[defaultString].InventoryRepositoryName].URL()),
invRepoDir)
}
func TestCurrentContextManifestMetadata(t *testing.T) { func TestCurrentContextManifestMetadata(t *testing.T) {
expectedMeta := &config.Metadata{ expectedMeta := &config.Metadata{
Inventory: &config.InventoryMeta{ Inventory: &config.InventoryMeta{

View File

@ -83,10 +83,11 @@ func (e ErrRepositoryNotFound) Error() string {
// ErrMissingRepositoryName is returned if repository name is empty // ErrMissingRepositoryName is returned if repository name is empty
// when using in set-manifest // when using in set-manifest
type ErrMissingRepositoryName struct { type ErrMissingRepositoryName struct {
RepoType string
} }
func (e ErrMissingRepositoryName) Error() string { func (e ErrMissingRepositoryName) Error() string {
return "Missing repository name." return fmt.Sprintf("Missing '%s' repository name.", e.RepoType)
} }
// ErrMissingRepoURL is returned if repository is empty // ErrMissingRepoURL is returned if repository is empty

View File

@ -22,6 +22,10 @@ type Manifest struct {
// PhaseRepositoryName is a name of the repo, that contains site/<site-name> directory // PhaseRepositoryName is a name of the repo, that contains site/<site-name> directory
// and is a starting point for building document bundle // and is a starting point for building document bundle
PhaseRepositoryName string `json:"phaseRepositoryName"` PhaseRepositoryName string `json:"phaseRepositoryName"`
// InventoryRepositoryName is a name of the repo contains inventory objects
// to be used mostly with baremetal deployments
// If not defined PhaseRepositoryName will be used to locate inventory
InventoryRepositoryName string `json:"inventoryRepositoryName"`
// ExtraRepositories is the map of extra repositories addressable by a name // ExtraRepositories is the map of extra repositories addressable by a name
Repositories map[string]*Repository `json:"repositories,omitempty"` Repositories map[string]*Repository `json:"repositories,omitempty"`
// TargetPath Local Target path for working or home directory for all Manifest Cloned/Returned/Generated // TargetPath Local Target path for working or home directory for all Manifest Cloned/Returned/Generated

View File

@ -17,6 +17,7 @@ managementConfiguration:
type: redfish type: redfish
manifests: manifests:
dummy_manifest: dummy_manifest:
inventoryRepositoryName: primary
metadataPath: metadata.yaml metadataPath: metadata.yaml
phaseRepositoryName: primary phaseRepositoryName: primary
repositories: repositories:

View File

@ -1,3 +1,4 @@
inventoryRepositoryName: primary
metadataPath: metadata.yaml metadataPath: metadata.yaml
phaseRepositoryName: primary phaseRepositoryName: primary
repositories: repositories:

View File

@ -49,9 +49,10 @@ func NewConfig() *Config {
}, },
}, },
}, },
TargetPath: "/tmp/" + AirshipDefaultManifest, TargetPath: "/tmp/" + AirshipDefaultManifest,
PhaseRepositoryName: DefaultTestPhaseRepo, PhaseRepositoryName: DefaultTestPhaseRepo,
MetadataPath: DefaultManifestMetadataFile, InventoryRepositoryName: DefaultTestPhaseRepo,
MetadataPath: DefaultManifestMetadataFile,
}, },
}, },
} }
@ -88,10 +89,11 @@ func NewContext() *Context {
// object with non-nil maps // object with non-nil maps
func NewManifest() *Manifest { func NewManifest() *Manifest {
return &Manifest{ return &Manifest{
PhaseRepositoryName: DefaultTestPhaseRepo, InventoryRepositoryName: DefaultTestPhaseRepo,
TargetPath: DefaultTargetPath, PhaseRepositoryName: DefaultTestPhaseRepo,
Repositories: map[string]*Repository{DefaultTestPhaseRepo: NewRepository()}, TargetPath: DefaultTargetPath,
MetadataPath: DefaultManifestMetadataFile, Repositories: map[string]*Repository{DefaultTestPhaseRepo: NewRepository()},
MetadataPath: DefaultManifestMetadataFile,
} }
} }

View File

@ -54,7 +54,7 @@ func (i Invetnory) BaremetalInventory() (ifc.BaremetalInventory, error) {
return nil, err return nil, err
} }
phaseDir, err := cfg.CurrentContextPhaseRepositoryDir() phaseDir, err := cfg.CurrentContextInventoryRepositoryName()
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -69,6 +69,7 @@ func TestBaremetalInventory(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
manifest.MetadataPath = "metadata.yaml" manifest.MetadataPath = "metadata.yaml"
manifest.PhaseRepositoryName = "testdata" manifest.PhaseRepositoryName = "testdata"
manifest.InventoryRepositoryName = "testdata"
manifest.Repositories["testdata"] = &config.Repository{ manifest.Repositories["testdata"] = &config.Repository{
URLString: "/myrepo/testdata", URLString: "/myrepo/testdata",
} }
@ -87,7 +88,7 @@ func TestBaremetalInventory(t *testing.T) {
require.Error(t, err) require.Error(t, err)
assert.Contains(t, err.Error(), tt.errString) assert.Contains(t, err.Error(), tt.errString)
} else { } else {
require.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, bmhInv) assert.NotNil(t, bmhInv)
} }
}) })

View File

@ -69,6 +69,7 @@ func DummyManifest() *config.Manifest {
// Repositories is the map of repository addressable by a name // Repositories is the map of repository addressable by a name
m.Repositories = map[string]*config.Repository{"primary": DummyRepository()} m.Repositories = map[string]*config.Repository{"primary": DummyRepository()}
m.PhaseRepositoryName = "primary" m.PhaseRepositoryName = "primary"
m.InventoryRepositoryName = "primary"
m.MetadataPath = "metadata.yaml" m.MetadataPath = "metadata.yaml"
m.TargetPath = "/var/tmp/" m.TargetPath = "/var/tmp/"
return m return m