diff options
Diffstat (limited to 'src/ncm/pkg/networkintents')
-rw-r--r-- | src/ncm/pkg/networkintents/network.go | 40 | ||||
-rw-r--r-- | src/ncm/pkg/networkintents/providernet.go | 48 |
2 files changed, 78 insertions, 10 deletions
diff --git a/src/ncm/pkg/networkintents/network.go b/src/ncm/pkg/networkintents/network.go index de8ee504..58480cc8 100644 --- a/src/ncm/pkg/networkintents/network.go +++ b/src/ncm/pkg/networkintents/network.go @@ -22,6 +22,7 @@ import ( nettypes "github.com/onap/multicloud-k8s/src/ncm/pkg/networkintents/types" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" mtypes "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/types" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/state" pkgerrors "github.com/pkg/errors" ) @@ -89,11 +90,25 @@ func (v *NetworkClient) CreateNetwork(p Network, clusterProvider, cluster string NetworkName: p.Metadata.Name, } - //Check if cluster exists - _, err := clusterPkg.NewClusterClient().GetCluster(clusterProvider, cluster) + //Check if cluster exists and in a state for adding network intents + s, err := clusterPkg.NewClusterClient().GetClusterState(clusterProvider, cluster) if err != nil { return Network{}, pkgerrors.New("Unable to find the cluster") } + switch s.State { + case state.StateEnum.Approved: + return Network{}, pkgerrors.Errorf("Cluster is in an invalid state: " + cluster + " " + state.StateEnum.Approved) + case state.StateEnum.Terminated: + break + case state.StateEnum.Created: + break + case state.StateEnum.Applied: + return Network{}, pkgerrors.Errorf("Existing cluster network intents must be terminated before creating: " + cluster) + case state.StateEnum.Instantiated: + return Network{}, pkgerrors.Errorf("Cluster is in an invalid state: " + cluster + " " + state.StateEnum.Instantiated) + default: + return Network{}, pkgerrors.Errorf("Cluster is in an invalid state: " + cluster + " " + s.State) + } //Check if this Network already exists _, err = v.GetNetwork(p.Metadata.Name, clusterProvider, cluster) @@ -167,6 +182,25 @@ func (v *NetworkClient) GetNetworks(clusterProvider, cluster string) ([]Network, // Delete the Network from database func (v *NetworkClient) DeleteNetwork(name, clusterProvider, cluster string) error { + // verify cluster is in a state where network intent can be deleted + s, err := clusterPkg.NewClusterClient().GetClusterState(clusterProvider, cluster) + if err != nil { + return pkgerrors.New("Unable to find the cluster") + } + switch s.State { + case state.StateEnum.Approved: + return pkgerrors.Errorf("Cluster is in an invalid state: " + cluster + " " + state.StateEnum.Approved) + case state.StateEnum.Terminated: + break + case state.StateEnum.Created: + break + case state.StateEnum.Applied: + return pkgerrors.Errorf("Cluster network intents must be terminated before deleting: " + cluster) + case state.StateEnum.Instantiated: + return pkgerrors.Errorf("Cluster is in an invalid state: " + cluster + " " + state.StateEnum.Instantiated) + default: + return pkgerrors.Errorf("Cluster is in an invalid state: " + cluster + " " + s.State) + } //Construct key and tag to select the entry key := NetworkKey{ @@ -175,7 +209,7 @@ func (v *NetworkClient) DeleteNetwork(name, clusterProvider, cluster string) err NetworkName: name, } - err := db.DBconn.Remove(v.db.StoreName, key) + err = db.DBconn.Remove(v.db.StoreName, key) if err != nil { return pkgerrors.Wrap(err, "Delete Network Entry;") } diff --git a/src/ncm/pkg/networkintents/providernet.go b/src/ncm/pkg/networkintents/providernet.go index 072e07f6..dbe6e46c 100644 --- a/src/ncm/pkg/networkintents/providernet.go +++ b/src/ncm/pkg/networkintents/providernet.go @@ -22,6 +22,7 @@ import ( nettypes "github.com/onap/multicloud-k8s/src/ncm/pkg/networkintents/types" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" mtypes "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/types" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/state" pkgerrors "github.com/pkg/errors" ) @@ -84,6 +85,26 @@ func NewProviderNetClient() *ProviderNetClient { // CreateProviderNet - create a new ProviderNet func (v *ProviderNetClient) CreateProviderNet(p ProviderNet, clusterProvider, cluster string, exists bool) (ProviderNet, error) { + // verify cluster exists and in state to add provider networks + s, err := clusterPkg.NewClusterClient().GetClusterState(clusterProvider, cluster) + if err != nil { + return ProviderNet{}, pkgerrors.New("Unable to find the cluster") + } + switch s.State { + case state.StateEnum.Approved: + return ProviderNet{}, pkgerrors.Wrap(err, "Cluster is in an invalid state: "+cluster+" "+state.StateEnum.Approved) + case state.StateEnum.Terminated: + break + case state.StateEnum.Created: + break + case state.StateEnum.Applied: + return ProviderNet{}, pkgerrors.Wrap(err, "Existing cluster provider network intents must be terminated before creating: "+cluster) + case state.StateEnum.Instantiated: + return ProviderNet{}, pkgerrors.Wrap(err, "Cluster is in an invalid state: "+cluster+" "+state.StateEnum.Instantiated) + default: + return ProviderNet{}, pkgerrors.Wrap(err, "Cluster is in an invalid state: "+cluster+" "+s.State) + } + //Construct key and tag to select the entry key := ProviderNetKey{ ClusterProviderName: clusterProvider, @@ -91,12 +112,6 @@ func (v *ProviderNetClient) CreateProviderNet(p ProviderNet, clusterProvider, cl ProviderNetName: p.Metadata.Name, } - //Check if cluster exists - _, err := clusterPkg.NewClusterClient().GetCluster(clusterProvider, cluster) - if err != nil { - return ProviderNet{}, pkgerrors.New("Unable to find the cluster") - } - //Check if this ProviderNet already exists _, err = v.GetProviderNet(p.Metadata.Name, clusterProvider, cluster) if err == nil && !exists { @@ -169,6 +184,25 @@ func (v *ProviderNetClient) GetProviderNets(clusterProvider, cluster string) ([] // Delete the ProviderNet from database func (v *ProviderNetClient) DeleteProviderNet(name, clusterProvider, cluster string) error { + // verify cluster is in a state where provider network intent can be deleted + s, err := clusterPkg.NewClusterClient().GetClusterState(clusterProvider, cluster) + if err != nil { + return pkgerrors.New("Unable to find the cluster") + } + switch s.State { + case state.StateEnum.Approved: + return pkgerrors.Wrap(err, "Cluster is in an invalid state: "+cluster+" "+state.StateEnum.Approved) + case state.StateEnum.Terminated: + break + case state.StateEnum.Created: + break + case state.StateEnum.Applied: + return pkgerrors.Wrap(err, "Cluster provider network intents must be terminated before deleting: "+cluster) + case state.StateEnum.Instantiated: + return pkgerrors.Wrap(err, "Cluster is in an invalid state: "+cluster+" "+state.StateEnum.Instantiated) + default: + return pkgerrors.Wrap(err, "Cluster is in an invalid state: "+cluster+" "+s.State) + } //Construct key and tag to select the entry key := ProviderNetKey{ @@ -177,7 +211,7 @@ func (v *ProviderNetClient) DeleteProviderNet(name, clusterProvider, cluster str ProviderNetName: name, } - err := db.DBconn.Remove(v.db.StoreName, key) + err = db.DBconn.Remove(v.db.StoreName, key) if err != nil { return pkgerrors.Wrap(err, "Delete ProviderNet Entry;") } |