diff --git a/README.md b/README.md index 519dbf0..715f3d9 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ But as oapi-cli allow to chain calls, having a way, to keep some argument taken To do so, here's how to do it using oapi-cli's variables: ```sh -CreateVms --ImageId IMG_NAME --set-var vm_id=Vms.0.VmId \ +oapi-cli CreateVms --ImageId IMG_NAME --set-var vm_id=Vms.0.VmId \ CreateTags --ResourceIds[] --var vm_id --Tags.0.Key Name ..Value "my vm" ``` @@ -100,7 +100,7 @@ Here `VmId` is store in `vm_id` oapi-cli variable using `--set-var vm_id=Vms.0.V `--set-var`, take a single argument, a string using the syntax `ID=JSON_PATH`. In order to use it, you need to know which part of a call return you want to store in a variable. - +more examples [here](./variable.md) # Config diff --git a/variable.md b/variable.md new file mode 100644 index 0000000..f1f4d70 --- /dev/null +++ b/variable.md @@ -0,0 +1,57 @@ + +Let's say you want to create a VM, called "joe" in a script. + +using osc-cli, you would have to do something like this: + +```sh +vm_id=$(osc-cli api CreateVms --ImageId $id | jq .Vms[0].VmId) +oapi-cli CreateTags --ResourceIds "[$vm_id]" --Tags '[ { "Key": "Name", "Value": "joe" } ]' +``` +Moreover if you want to retrieve the ImageId programmatically, You need another call to ReadImage: +```sh +image_id=$(osc-cli api ReadImages --Filters '{"ImageNames": ["RockyLinux*"]}' | jq .Images[0].ImageId) +vm_id=$(osc-cli api CreateVms --ImageId $image_id | jq .Vms[0].VmId) +oapi-cli CreateTags --ResourceIds "[$vm_id]" --Tags '[ { "Key": "Name", "Value": "joe" } ]' +``` + +With oapi-cli you can now chain calls, so you don't have to keep intermediary variables. + +```sh +oapi-cli ReadImages --Filter.ImageNames[] "RockyLinux*" --set-var img_id=Images.0.ImageId \ + CreateVms --ImageId --var img_id --set-var vm_id=Vms.0.VmId \ + CreateTags --ResourceIds[] --var vm_id --Tags.0.Key Name ..Value "my vm" +``` + +New let's create a Net, a SecurityGroups, and add rule on it. + +```sh +net=$(osc-cli --endpoint "http://127.0.0.1:3000" api CreateNet --IpRange "10.0.0.0/16" | jq .Net.NetId) + +sg=$(osc-cli --endpoint "http://127.0.0.1:3000" api CreateSecurityGroup \ + --NetId $net \ + --SecurityGroupName "security-group-example" \ + --Description "Security group example" | jq .SecurityGroup.SecurityGroupId) + +osc-cli --endpoint "http://127.0.0.1:3000" api CreateSecurityGroupRule \ + --Flow "Inbound" \ + --SecurityGroupId $sg \ + --Rules '[ + { + "FromPortRange": 22, + "ToPortRange": 22, + "IpProtocol": "tcp", + "SecurityGroupsMembers": [{"AccountId": "123456789012", "SecurityGroupName": "another-security-group"}], + }, + ]' +``` + +with oapi-cli +```sh +oapi-cli CreateNet --IpRange "10.0.0.0/16" --set-var net=Net.NetId \ + CreateSecurityGroup --NetId --var net --SecurityGroupName "security-group-example"\ + --Description "Security group example" --set-var sg=SecurityGroup.SecurityGroupId \ + CreateSecurityGroupRule --Flow "Inbound" --SecurityGroupId --var sg \ + --Rules.0.FromPortRange 22 ..ToPortRange 22 ..IpProtocol tcp \ + ..SecurityGroupsMembers.0.AccountId 123456789012 \ + ..SecurityGroupName another-security-group +```