Skip to content

Commit

Permalink
Adds metadata to deployment and get contracts via graphql
Browse files Browse the repository at this point in the history
Signed-off-by: Ashraf Fouda <[email protected]>
  • Loading branch information
ashraffouda committed Sep 17, 2023
1 parent fa85e34 commit 6d797d0
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 4 deletions.
2 changes: 1 addition & 1 deletion v2go2tgrid/grid_cli/rmb.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func deploymentDeploy(mnemonics string, substrate_url string, relay_url string,
var dl gridtypes.Deployment
err = json.Unmarshal([]byte(data), &dl)
if err != nil {
return fmt.Errorf("failed to marshal deployment %w", err)
return fmt.Errorf("failed to unmarshal deployment %w", err)
}

if err := client.Call(ctx, dst, "zos.deployment.deploy", dl, nil); err != nil {
Expand Down
2 changes: 0 additions & 2 deletions v2go2tgrid/tfgrid/deployer.v
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ pub fn (mut d Deployer) wait_deployment(node_id u32, contract_id u64, workload_v
for {
mut state_ok := 0
changes := d.deployment_changes(node_id, contract_id)!
println('got ${changes.len} workloads')
for wl in changes {
println('Workload: ${wl.name}, State: ${wl.result.state}, version: ${wl.version}')
if wl.version == workload_versions[wl.name] && wl.result.state == result_states.ok {
state_ok++
} else if wl.version == workload_versions[wl.name] && wl.result.state == result_states.error {
Expand Down
27 changes: 26 additions & 1 deletion v2go2tgrid/tfgrid/deployment.v
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ pub struct DeploymentArgs {
}

pub fn (mut deployment Deployment) challenge() string {
// we need to scape `"` with `\"`char when sending the payload to be a valid json but when calculating the challenge we should remove `\` so we don't get invlaid signature
metadata := deployment.metadata.replace('\\"', '"')
mut out := []string{}
out << '${deployment.version}'
out << '${deployment.twin_id}'
out << '${deployment.metadata}'
out << '${metadata}'
out << '${deployment.description}'
out << '${deployment.expiration}'
for mut workload in deployment.workloads {
Expand Down Expand Up @@ -158,3 +160,26 @@ pub fn new_deployment(args DeploymentArgs) Deployment {
signature_requirement: args.signature_requirement
}
}

struct DeploymentData {
type_ string
name string
project_name string
}

pub fn (mut data DeploymentData) json_encode() string {
return "{\\\"type\\\":\\\"${data.type_}\\\",\\\"name\\\":\\\"${data.name}\\\",\\\"projectName\\\":\\\"${data.project_name}\\\"}"
}

pub fn (mut dl Deployment) add_metadata(type_ string, project_name string) {
mut data := DeploymentData{
type_: type_
name: project_name
project_name: project_name
}
dl.metadata = data.json_encode()
}

pub fn (mut d Deployment) parse_metadata() !DeploymentData {
return json.decode(DeploymentData, d.metadata)!
}
2 changes: 2 additions & 0 deletions v2go2tgrid/tfgrid/examples/deploy_vm.v
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ fn main() {

mut deployment := tfgrid.new_deployment(
twin_id: deployer.twin_id
description: 'test deployment'
workloads: [znet_workload, zmachine_workload]
signature_requirement: signature_requirement
)
deployment.add_metadata('vm', 'SimpleVM')

contract_id := deployer.deploy(node_id, mut deployment, '', 0) or {
logger.error('failed to deploy deployment: ${err}')
Expand Down
95 changes: 95 additions & 0 deletions v2go2tgrid/tfgrid/graphql.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
module tfgrid

import net.http
import json
import x.json2

pub struct GraphQl {
url string
}

pub struct Contract {
contract_id string [json: contractID]
deployment_data string [json: deploymentData]
state string
node_id u32 [json: nodeID]
}

pub struct Contracts {
name_contracts []Contract [json: nameContracts]
node_contracts []Contract [json: nodeContracts]
rent_contracts []Contract [json: rentContracts]
}

// contractsList, err := c.ListContractsByTwinID([]string{"Created, GracePeriod"})
pub fn (g GraphQl) list_twin_contracts(twin_id u32, states []string) !Contracts {
state := states.join(', ')
options := '(where: {twinID_eq: ${twin_id}, state_in: ${state}}, orderBy: twinID_ASC)'
name_contracts_count := g.get_item_total_count('nameContracts', options)!
node_contracts_count := g.get_item_total_count('nodeContracts', options)!
rent_contracts_count := g.get_item_total_count('rentContracts', options)!

contracts_data := g.query('query getContracts(\$nameContractsCount: Int!, \$nodeContractsCount: Int!, \$rentContractsCount: Int!){
nameContracts(where: {twinID_eq: ${twin_id}, state_in: ${state}}, limit: \$nameContractsCount) {
contractID
state
name
}
nodeContracts(where: {twinID_eq: ${twin_id}, state_in: ${state}}, limit: \$nodeContractsCount) {
contractID
deploymentData
state
nodeID
}
rentContracts(where: {twinID_eq: ${twin_id}, state_in: ${state}}, limit: \$rentContractsCount) {
contractID
state
nodeID
}
}', // map[string]u32{}
{
'nodeContractsCount': node_contracts_count
'nameContractsCount': name_contracts_count
'rentContractsCount': rent_contracts_count
})!

return json.decode(Contracts, contracts_data.str())!
}

// GetItemTotalCount return count of items
fn (g GraphQl) get_item_total_count(item_name string, options string) !u32 {
count_body := 'query { items: ${item_name}Connection${options} { count: totalCount } }'
request_body := {
'query': count_body
}
json_body := json.encode(request_body)

resp := http.post_json(g.url, json_body)!
query_data := json2.raw_decode(resp.body)!
query_map := query_data.as_map()
data := query_map['data']! as map[string]json2.Any
items := data['items']! as map[string]json2.Any
count := u32(items['count']!.int())
return count
}

struct QueryRequest {
query string
variables map[string]u32
}

// Query queries graphql
fn (g GraphQl) query(body string, variables map[string]u32) !map[string]json2.Any {
mut request_body := QueryRequest{
query: body
variables: variables
}
json_body := json.encode(request_body)
resp := http.post_json(g.url, json_body)!
println(' ${resp.body}')

query_data := json2.raw_decode(resp.body)!
data_map := query_data.as_map()
result := data_map['data']!.as_map()
return result
}

0 comments on commit 6d797d0

Please sign in to comment.