From cb0065078034def2a77086930f646e6eaa2a99db Mon Sep 17 00:00:00 2001 From: rawdaGastan Date: Sun, 9 Jun 2024 14:13:33 +0300 Subject: [PATCH 1/2] get contract metadata if deployment metadata is missing, create contractsGetter interface, remove deprectaed megacheck linter --- .golangci.yml | 1 - grid-cli/cmd/cancel_contracts.go | 2 +- grid-client/deployer/tf_plugin_client.go | 9 +- grid-client/graphql/contracts.go | 37 ++++++++ grid-client/mocks/graphql.go | 105 +++++++++++++++++++++++ grid-client/state/state.go | 26 +++++- grid-client/state/state_test.go | 3 +- 7 files changed, 173 insertions(+), 10 deletions(-) create mode 100644 grid-client/mocks/graphql.go diff --git a/.golangci.yml b/.golangci.yml index 8deff7229..737ac55e1 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -5,7 +5,6 @@ linters: - gofmt - govet - ineffassign - - megacheck - misspell - unconvert enable-all: false diff --git a/grid-cli/cmd/cancel_contracts.go b/grid-cli/cmd/cancel_contracts.go index 6942eebec..12e2a6c57 100644 --- a/grid-cli/cmd/cancel_contracts.go +++ b/grid-cli/cmd/cancel_contracts.go @@ -59,7 +59,7 @@ func init() { cancelContracts.Flags().BoolP("all", "a", false, "delete all contracts") } -func getAllContracts(getter graphql.ContractsGetter) ([]uint64, error) { +func getAllContracts(getter graphql.ContractsGetterI) ([]uint64, error) { var contractIDs []uint64 cs, err := getter.ListContractsByTwinID([]string{"Created"}) if err != nil { diff --git a/grid-client/deployer/tf_plugin_client.go b/grid-client/deployer/tf_plugin_client.go index 896c5ddb7..7832bb006 100644 --- a/grid-client/deployer/tf_plugin_client.go +++ b/grid-client/deployer/tf_plugin_client.go @@ -98,7 +98,7 @@ type TFPluginClient struct { // contracts graphQl graphql.GraphQl - ContractsGetter graphql.ContractsGetter + ContractsGetter graphql.ContractsGetterI // calculator Calculator calculator.Calculator @@ -333,15 +333,16 @@ func NewTFPluginClient( tfPluginClient.K8sDeployer = NewK8sDeployer(&tfPluginClient) tfPluginClient.GatewayNameDeployer = NewGatewayNameDeployer(&tfPluginClient) - tfPluginClient.State = state.NewState(tfPluginClient.NcPool, tfPluginClient.SubstrateConn) - graphqlURL := GraphQlURLs[cfg.network] tfPluginClient.graphQl, err = graphql.NewGraphQl(graphqlURL) if err != nil { return TFPluginClient{}, errors.Wrapf(err, "could not create a new graphql with url: %s", graphqlURL) } - tfPluginClient.ContractsGetter = graphql.NewContractsGetter(tfPluginClient.TwinID, tfPluginClient.graphQl, tfPluginClient.SubstrateConn, tfPluginClient.NcPool) + contractsGetter := graphql.NewContractsGetter(tfPluginClient.TwinID, tfPluginClient.graphQl, tfPluginClient.SubstrateConn, tfPluginClient.NcPool) + tfPluginClient.ContractsGetter = &contractsGetter + + tfPluginClient.State = state.NewState(tfPluginClient.NcPool, tfPluginClient.SubstrateConn, tfPluginClient.ContractsGetter) tfPluginClient.Calculator = calculator.NewCalculator(tfPluginClient.SubstrateConn, tfPluginClient.Identity) diff --git a/grid-client/graphql/contracts.go b/grid-client/graphql/contracts.go index 9888b271c..502f130e2 100644 --- a/grid-client/graphql/contracts.go +++ b/grid-client/graphql/contracts.go @@ -17,6 +17,13 @@ import ( "github.com/threefoldtech/zos/pkg/gridtypes/zos" ) +type ContractsGetterI interface { + ListContractsByTwinID(states []string) (Contracts, error) + ListContractsOfProjectName(projectName string, noGateways ...bool) (Contracts, error) + GetContractByID(contractID string, noGateways ...bool) (Contracts, error) + GetNodeContractsByTypeAndName(projectName, deploymentType, deploymentName string) (map[uint32]uint64, error) +} + // ContractsGetter for contracts getter from graphql type ContractsGetter struct { twinID uint32 @@ -117,6 +124,36 @@ func (c *ContractsGetter) ListContractsByTwinID(states []string) (Contracts, err return listContracts, nil } +// GetContractByID returns contract for an ID +func (c *ContractsGetter) GetContractByID(contractID string, noGateways ...bool) (Contracts, error) { + contracts := Contracts{ + NodeContracts: make([]Contract, 0), + NameContracts: make([]Contract, 0), + } + contractsList, err := c.ListContractsByTwinID([]string{"Created, GracePeriod"}) + if err != nil { + return Contracts{}, err + } + + for _, contract := range contractsList.NodeContracts { + if contractID == contract.ContractID { + contracts.NodeContracts = append(contracts.NodeContracts, contract) + } + } + + if len(noGateways) > 0 && noGateways[0] { + return contracts, nil + } + + nameGatewaysWorkloads, err := c.filterNameGatewaysWithinNodeContracts(contracts.NodeContracts) + if err != nil { + return Contracts{}, err + } + + contracts.NameContracts = c.filterNameContracts(contractsList.NameContracts, nameGatewaysWorkloads) + return contracts, nil +} + // ListContractsOfProjectName returns contracts for a project name func (c *ContractsGetter) ListContractsOfProjectName(projectName string, noGateways ...bool) (Contracts, error) { contracts := Contracts{ diff --git a/grid-client/mocks/graphql.go b/grid-client/mocks/graphql.go new file mode 100644 index 000000000..5912328d4 --- /dev/null +++ b/grid-client/mocks/graphql.go @@ -0,0 +1,105 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: graphql/contracts.go + +// Package mocks is a generated GoMock package. +package mocks + +import ( + reflect "reflect" + + gomock "github.com/golang/mock/gomock" + graphql "github.com/threefoldtech/tfgrid-sdk-go/grid-client/graphql" +) + +// MockContractsGetterI is a mock of ContractsGetterI interface. +type MockContractsGetterI struct { + ctrl *gomock.Controller + recorder *MockContractsGetterIMockRecorder +} + +// MockContractsGetterIMockRecorder is the mock recorder for MockContractsGetterI. +type MockContractsGetterIMockRecorder struct { + mock *MockContractsGetterI +} + +// NewMockContractsGetterI creates a new mock instance. +func NewMockContractsGetterI(ctrl *gomock.Controller) *MockContractsGetterI { + mock := &MockContractsGetterI{ctrl: ctrl} + mock.recorder = &MockContractsGetterIMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockContractsGetterI) EXPECT() *MockContractsGetterIMockRecorder { + return m.recorder +} + +// GetContractByID mocks base method. +func (m *MockContractsGetterI) GetContractByID(contractID string, noGateways ...bool) (graphql.Contracts, error) { + m.ctrl.T.Helper() + varargs := []interface{}{contractID} + for _, a := range noGateways { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "GetContractByID", varargs...) + ret0, _ := ret[0].(graphql.Contracts) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetContractByID indicates an expected call of GetContractByID. +func (mr *MockContractsGetterIMockRecorder) GetContractByID(contractID interface{}, noGateways ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{contractID}, noGateways...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetContractByID", reflect.TypeOf((*MockContractsGetterI)(nil).GetContractByID), varargs...) +} + +// GetNodeContractsByTypeAndName mocks base method. +func (m *MockContractsGetterI) GetNodeContractsByTypeAndName(projectName, deploymentType, deploymentName string) (map[uint32]uint64, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetNodeContractsByTypeAndName", projectName, deploymentType, deploymentName) + ret0, _ := ret[0].(map[uint32]uint64) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetNodeContractsByTypeAndName indicates an expected call of GetNodeContractsByTypeAndName. +func (mr *MockContractsGetterIMockRecorder) GetNodeContractsByTypeAndName(projectName, deploymentType, deploymentName interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetNodeContractsByTypeAndName", reflect.TypeOf((*MockContractsGetterI)(nil).GetNodeContractsByTypeAndName), projectName, deploymentType, deploymentName) +} + +// ListContractsByTwinID mocks base method. +func (m *MockContractsGetterI) ListContractsByTwinID(states []string) (graphql.Contracts, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListContractsByTwinID", states) + ret0, _ := ret[0].(graphql.Contracts) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListContractsByTwinID indicates an expected call of ListContractsByTwinID. +func (mr *MockContractsGetterIMockRecorder) ListContractsByTwinID(states interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListContractsByTwinID", reflect.TypeOf((*MockContractsGetterI)(nil).ListContractsByTwinID), states) +} + +// ListContractsOfProjectName mocks base method. +func (m *MockContractsGetterI) ListContractsOfProjectName(projectName string, noGateways ...bool) (graphql.Contracts, error) { + m.ctrl.T.Helper() + varargs := []interface{}{projectName} + for _, a := range noGateways { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "ListContractsOfProjectName", varargs...) + ret0, _ := ret[0].(graphql.Contracts) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListContractsOfProjectName indicates an expected call of ListContractsOfProjectName. +func (mr *MockContractsGetterIMockRecorder) ListContractsOfProjectName(projectName interface{}, noGateways ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{projectName}, noGateways...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListContractsOfProjectName", reflect.TypeOf((*MockContractsGetterI)(nil).ListContractsOfProjectName), varargs...) +} diff --git a/grid-client/state/state.go b/grid-client/state/state.go index ec58d8ea4..5357afb44 100644 --- a/grid-client/state/state.go +++ b/grid-client/state/state.go @@ -7,8 +7,10 @@ import ( "fmt" "reflect" "slices" + "strings" "github.com/pkg/errors" + "github.com/threefoldtech/tfgrid-sdk-go/grid-client/graphql" client "github.com/threefoldtech/tfgrid-sdk-go/grid-client/node" "github.com/threefoldtech/tfgrid-sdk-go/grid-client/subi" "github.com/threefoldtech/tfgrid-sdk-go/grid-client/workloads" @@ -27,20 +29,22 @@ type State struct { Networks NetworkState - NcPool client.NodeClientGetter - Substrate subi.SubstrateExt + NcPool client.NodeClientGetter + Substrate subi.SubstrateExt + ContractsGetter graphql.ContractsGetterI } // ErrNotFound for state not found instances var ErrNotFound = errors.New("not found") // NewState generates a new state -func NewState(ncPool client.NodeClientGetter, substrate subi.SubstrateExt) *State { +func NewState(ncPool client.NodeClientGetter, substrate subi.SubstrateExt, contractsGetter graphql.ContractsGetterI) *State { return &State{ CurrentNodeDeployments: make(map[uint32]ContractIDs), Networks: NetworkState{State: make(map[string]Network)}, NcPool: ncPool, Substrate: substrate, + ContractsGetter: contractsGetter, } } @@ -297,6 +301,14 @@ func (st *State) LoadNetworkFromGrid(ctx context.Context, name string) (znet wor return znet, errors.Wrapf(err, "could not get network deployment %d from node %d", contractID, nodeID) } + if len(strings.TrimSpace(dl.Metadata)) == 0 { + contracts, err := st.ContractsGetter.GetContractByID(fmt.Sprint(contractID)) + if err != nil { + return znet, errors.Wrapf(err, "could not get network contract %d from node %d", contractID, nodeID) + } + dl.Metadata = contracts.NodeContracts[0].DeploymentData + } + deploymentData, err := workloads.ParseDeploymentData(dl.Metadata) if err != nil { return znet, errors.Wrapf(err, "could not generate deployment metadata for %s", name) @@ -403,6 +415,14 @@ func (st *State) GetWorkloadInDeployment(ctx context.Context, nodeID uint32, nam return gridtypes.Workload{}, gridtypes.Deployment{}, errors.Wrapf(err, "could not get deployment %d from node %d", contractID, nodeID) } + if len(strings.TrimSpace(dl.Metadata)) == 0 { + contracts, err := st.ContractsGetter.GetContractByID(fmt.Sprint(contractID)) + if err != nil { + return gridtypes.Workload{}, gridtypes.Deployment{}, errors.Wrapf(err, "could not get contract %d from node %d", contractID, nodeID) + } + dl.Metadata = contracts.NodeContracts[0].DeploymentData + } + dlData, err := workloads.ParseDeploymentData(dl.Metadata) if err != nil { return gridtypes.Workload{}, gridtypes.Deployment{}, errors.Wrapf(err, "could not get deployment %d data", contractID) diff --git a/grid-client/state/state_test.go b/grid-client/state/state_test.go index 80f98f96f..bc00a99a8 100644 --- a/grid-client/state/state_test.go +++ b/grid-client/state/state_test.go @@ -28,8 +28,9 @@ func SetupLoaderTests(t *testing.T, wls []gridtypes.Workload) *State { cl := mocks.NewRMBMockClient(ctrl) sub := mocks.NewMockSubstrateExt(ctrl) ncPool := mocks.NewMockNodeClientGetter(ctrl) + contractsGetter := mocks.NewMockContractsGetterI(ctrl) - state := NewState(ncPool, sub) + state := NewState(ncPool, sub, contractsGetter) state.CurrentNodeDeployments = map[uint32]ContractIDs{1: []uint64{10}} dl1 := workloads.NewGridDeployment(13, wls) From 5560749d1afb5fa0efced73332ccc80f8c896e27 Mon Sep 17 00:00:00 2001 From: rawdaGastan Date: Sun, 9 Jun 2024 14:55:59 +0300 Subject: [PATCH 2/2] use chain to get contract metadata instead of graphql --- grid-cli/cmd/cancel_contracts.go | 2 +- grid-client/deployer/tf_plugin_client.go | 7 +- grid-client/graphql/contracts.go | 37 -------- grid-client/mocks/graphql.go | 105 ----------------------- grid-client/state/state.go | 25 +++--- grid-client/state/state_test.go | 3 +- 6 files changed, 19 insertions(+), 160 deletions(-) delete mode 100644 grid-client/mocks/graphql.go diff --git a/grid-cli/cmd/cancel_contracts.go b/grid-cli/cmd/cancel_contracts.go index 12e2a6c57..6942eebec 100644 --- a/grid-cli/cmd/cancel_contracts.go +++ b/grid-cli/cmd/cancel_contracts.go @@ -59,7 +59,7 @@ func init() { cancelContracts.Flags().BoolP("all", "a", false, "delete all contracts") } -func getAllContracts(getter graphql.ContractsGetterI) ([]uint64, error) { +func getAllContracts(getter graphql.ContractsGetter) ([]uint64, error) { var contractIDs []uint64 cs, err := getter.ListContractsByTwinID([]string{"Created"}) if err != nil { diff --git a/grid-client/deployer/tf_plugin_client.go b/grid-client/deployer/tf_plugin_client.go index 7832bb006..d33467aa6 100644 --- a/grid-client/deployer/tf_plugin_client.go +++ b/grid-client/deployer/tf_plugin_client.go @@ -98,7 +98,7 @@ type TFPluginClient struct { // contracts graphQl graphql.GraphQl - ContractsGetter graphql.ContractsGetterI + ContractsGetter graphql.ContractsGetter // calculator Calculator calculator.Calculator @@ -339,10 +339,9 @@ func NewTFPluginClient( return TFPluginClient{}, errors.Wrapf(err, "could not create a new graphql with url: %s", graphqlURL) } - contractsGetter := graphql.NewContractsGetter(tfPluginClient.TwinID, tfPluginClient.graphQl, tfPluginClient.SubstrateConn, tfPluginClient.NcPool) - tfPluginClient.ContractsGetter = &contractsGetter + tfPluginClient.ContractsGetter = graphql.NewContractsGetter(tfPluginClient.TwinID, tfPluginClient.graphQl, tfPluginClient.SubstrateConn, tfPluginClient.NcPool) - tfPluginClient.State = state.NewState(tfPluginClient.NcPool, tfPluginClient.SubstrateConn, tfPluginClient.ContractsGetter) + tfPluginClient.State = state.NewState(tfPluginClient.NcPool, tfPluginClient.SubstrateConn) tfPluginClient.Calculator = calculator.NewCalculator(tfPluginClient.SubstrateConn, tfPluginClient.Identity) diff --git a/grid-client/graphql/contracts.go b/grid-client/graphql/contracts.go index 502f130e2..9888b271c 100644 --- a/grid-client/graphql/contracts.go +++ b/grid-client/graphql/contracts.go @@ -17,13 +17,6 @@ import ( "github.com/threefoldtech/zos/pkg/gridtypes/zos" ) -type ContractsGetterI interface { - ListContractsByTwinID(states []string) (Contracts, error) - ListContractsOfProjectName(projectName string, noGateways ...bool) (Contracts, error) - GetContractByID(contractID string, noGateways ...bool) (Contracts, error) - GetNodeContractsByTypeAndName(projectName, deploymentType, deploymentName string) (map[uint32]uint64, error) -} - // ContractsGetter for contracts getter from graphql type ContractsGetter struct { twinID uint32 @@ -124,36 +117,6 @@ func (c *ContractsGetter) ListContractsByTwinID(states []string) (Contracts, err return listContracts, nil } -// GetContractByID returns contract for an ID -func (c *ContractsGetter) GetContractByID(contractID string, noGateways ...bool) (Contracts, error) { - contracts := Contracts{ - NodeContracts: make([]Contract, 0), - NameContracts: make([]Contract, 0), - } - contractsList, err := c.ListContractsByTwinID([]string{"Created, GracePeriod"}) - if err != nil { - return Contracts{}, err - } - - for _, contract := range contractsList.NodeContracts { - if contractID == contract.ContractID { - contracts.NodeContracts = append(contracts.NodeContracts, contract) - } - } - - if len(noGateways) > 0 && noGateways[0] { - return contracts, nil - } - - nameGatewaysWorkloads, err := c.filterNameGatewaysWithinNodeContracts(contracts.NodeContracts) - if err != nil { - return Contracts{}, err - } - - contracts.NameContracts = c.filterNameContracts(contractsList.NameContracts, nameGatewaysWorkloads) - return contracts, nil -} - // ListContractsOfProjectName returns contracts for a project name func (c *ContractsGetter) ListContractsOfProjectName(projectName string, noGateways ...bool) (Contracts, error) { contracts := Contracts{ diff --git a/grid-client/mocks/graphql.go b/grid-client/mocks/graphql.go deleted file mode 100644 index 5912328d4..000000000 --- a/grid-client/mocks/graphql.go +++ /dev/null @@ -1,105 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: graphql/contracts.go - -// Package mocks is a generated GoMock package. -package mocks - -import ( - reflect "reflect" - - gomock "github.com/golang/mock/gomock" - graphql "github.com/threefoldtech/tfgrid-sdk-go/grid-client/graphql" -) - -// MockContractsGetterI is a mock of ContractsGetterI interface. -type MockContractsGetterI struct { - ctrl *gomock.Controller - recorder *MockContractsGetterIMockRecorder -} - -// MockContractsGetterIMockRecorder is the mock recorder for MockContractsGetterI. -type MockContractsGetterIMockRecorder struct { - mock *MockContractsGetterI -} - -// NewMockContractsGetterI creates a new mock instance. -func NewMockContractsGetterI(ctrl *gomock.Controller) *MockContractsGetterI { - mock := &MockContractsGetterI{ctrl: ctrl} - mock.recorder = &MockContractsGetterIMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockContractsGetterI) EXPECT() *MockContractsGetterIMockRecorder { - return m.recorder -} - -// GetContractByID mocks base method. -func (m *MockContractsGetterI) GetContractByID(contractID string, noGateways ...bool) (graphql.Contracts, error) { - m.ctrl.T.Helper() - varargs := []interface{}{contractID} - for _, a := range noGateways { - varargs = append(varargs, a) - } - ret := m.ctrl.Call(m, "GetContractByID", varargs...) - ret0, _ := ret[0].(graphql.Contracts) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetContractByID indicates an expected call of GetContractByID. -func (mr *MockContractsGetterIMockRecorder) GetContractByID(contractID interface{}, noGateways ...interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - varargs := append([]interface{}{contractID}, noGateways...) - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetContractByID", reflect.TypeOf((*MockContractsGetterI)(nil).GetContractByID), varargs...) -} - -// GetNodeContractsByTypeAndName mocks base method. -func (m *MockContractsGetterI) GetNodeContractsByTypeAndName(projectName, deploymentType, deploymentName string) (map[uint32]uint64, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetNodeContractsByTypeAndName", projectName, deploymentType, deploymentName) - ret0, _ := ret[0].(map[uint32]uint64) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetNodeContractsByTypeAndName indicates an expected call of GetNodeContractsByTypeAndName. -func (mr *MockContractsGetterIMockRecorder) GetNodeContractsByTypeAndName(projectName, deploymentType, deploymentName interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetNodeContractsByTypeAndName", reflect.TypeOf((*MockContractsGetterI)(nil).GetNodeContractsByTypeAndName), projectName, deploymentType, deploymentName) -} - -// ListContractsByTwinID mocks base method. -func (m *MockContractsGetterI) ListContractsByTwinID(states []string) (graphql.Contracts, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ListContractsByTwinID", states) - ret0, _ := ret[0].(graphql.Contracts) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// ListContractsByTwinID indicates an expected call of ListContractsByTwinID. -func (mr *MockContractsGetterIMockRecorder) ListContractsByTwinID(states interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListContractsByTwinID", reflect.TypeOf((*MockContractsGetterI)(nil).ListContractsByTwinID), states) -} - -// ListContractsOfProjectName mocks base method. -func (m *MockContractsGetterI) ListContractsOfProjectName(projectName string, noGateways ...bool) (graphql.Contracts, error) { - m.ctrl.T.Helper() - varargs := []interface{}{projectName} - for _, a := range noGateways { - varargs = append(varargs, a) - } - ret := m.ctrl.Call(m, "ListContractsOfProjectName", varargs...) - ret0, _ := ret[0].(graphql.Contracts) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// ListContractsOfProjectName indicates an expected call of ListContractsOfProjectName. -func (mr *MockContractsGetterIMockRecorder) ListContractsOfProjectName(projectName interface{}, noGateways ...interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - varargs := append([]interface{}{projectName}, noGateways...) - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListContractsOfProjectName", reflect.TypeOf((*MockContractsGetterI)(nil).ListContractsOfProjectName), varargs...) -} diff --git a/grid-client/state/state.go b/grid-client/state/state.go index 5357afb44..ae8ce4933 100644 --- a/grid-client/state/state.go +++ b/grid-client/state/state.go @@ -10,7 +10,6 @@ import ( "strings" "github.com/pkg/errors" - "github.com/threefoldtech/tfgrid-sdk-go/grid-client/graphql" client "github.com/threefoldtech/tfgrid-sdk-go/grid-client/node" "github.com/threefoldtech/tfgrid-sdk-go/grid-client/subi" "github.com/threefoldtech/tfgrid-sdk-go/grid-client/workloads" @@ -29,22 +28,20 @@ type State struct { Networks NetworkState - NcPool client.NodeClientGetter - Substrate subi.SubstrateExt - ContractsGetter graphql.ContractsGetterI + NcPool client.NodeClientGetter + Substrate subi.SubstrateExt } // ErrNotFound for state not found instances var ErrNotFound = errors.New("not found") // NewState generates a new state -func NewState(ncPool client.NodeClientGetter, substrate subi.SubstrateExt, contractsGetter graphql.ContractsGetterI) *State { +func NewState(ncPool client.NodeClientGetter, substrate subi.SubstrateExt) *State { return &State{ CurrentNodeDeployments: make(map[uint32]ContractIDs), Networks: NetworkState{State: make(map[string]Network)}, NcPool: ncPool, Substrate: substrate, - ContractsGetter: contractsGetter, } } @@ -302,11 +299,14 @@ func (st *State) LoadNetworkFromGrid(ctx context.Context, name string) (znet wor } if len(strings.TrimSpace(dl.Metadata)) == 0 { - contracts, err := st.ContractsGetter.GetContractByID(fmt.Sprint(contractID)) + contract, err := sub.GetContract(contractID) if err != nil { - return znet, errors.Wrapf(err, "could not get network contract %d from node %d", contractID, nodeID) + return znet, errors.Wrapf(err, "could not get contract %d from node %d", contractID, nodeID) + } + dl.Metadata = contract.ContractType.NodeContract.DeploymentData + if len(strings.TrimSpace(dl.Metadata)) == 0 { + return znet, errors.Wrapf(err, "contract %d doesn't have metadata", contractID) } - dl.Metadata = contracts.NodeContracts[0].DeploymentData } deploymentData, err := workloads.ParseDeploymentData(dl.Metadata) @@ -416,11 +416,14 @@ func (st *State) GetWorkloadInDeployment(ctx context.Context, nodeID uint32, nam } if len(strings.TrimSpace(dl.Metadata)) == 0 { - contracts, err := st.ContractsGetter.GetContractByID(fmt.Sprint(contractID)) + contract, err := sub.GetContract(contractID) if err != nil { return gridtypes.Workload{}, gridtypes.Deployment{}, errors.Wrapf(err, "could not get contract %d from node %d", contractID, nodeID) } - dl.Metadata = contracts.NodeContracts[0].DeploymentData + dl.Metadata = contract.ContractType.NodeContract.DeploymentData + if len(strings.TrimSpace(dl.Metadata)) == 0 { + return gridtypes.Workload{}, gridtypes.Deployment{}, errors.Wrapf(err, "contract %d doesn't have metadata", contractID) + } } dlData, err := workloads.ParseDeploymentData(dl.Metadata) diff --git a/grid-client/state/state_test.go b/grid-client/state/state_test.go index bc00a99a8..80f98f96f 100644 --- a/grid-client/state/state_test.go +++ b/grid-client/state/state_test.go @@ -28,9 +28,8 @@ func SetupLoaderTests(t *testing.T, wls []gridtypes.Workload) *State { cl := mocks.NewRMBMockClient(ctrl) sub := mocks.NewMockSubstrateExt(ctrl) ncPool := mocks.NewMockNodeClientGetter(ctrl) - contractsGetter := mocks.NewMockContractsGetterI(ctrl) - state := NewState(ncPool, sub, contractsGetter) + state := NewState(ncPool, sub) state.CurrentNodeDeployments = map[uint32]ContractIDs{1: []uint64{10}} dl1 := workloads.NewGridDeployment(13, wls)