|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "math/rand" |
| 8 | + "time" |
| 9 | + |
| 10 | + v "github.com/IBM-Cloud/power-go-client/clients/instance" |
| 11 | + "github.com/IBM-Cloud/power-go-client/helpers" |
| 12 | + ps "github.com/IBM-Cloud/power-go-client/ibmpisession" |
| 13 | + "github.com/IBM-Cloud/power-go-client/power/models" |
| 14 | + "github.com/IBM/go-sdk-core/v5/core" |
| 15 | +) |
| 16 | + |
| 17 | +func main() { |
| 18 | + //session Inputs |
| 19 | + // < IAM TOKEN > |
| 20 | + // token := "" |
| 21 | + region := "dal" |
| 22 | + zone := "dal10" |
| 23 | + accountID := "efe5e8b9d3f04b948790fe5499bd18bc" |
| 24 | + url := region + ".power-iaas.test.cloud.ibm.com" |
| 25 | + |
| 26 | + // volume inputs |
| 27 | + rand.Seed(time.Now().UnixNano()) |
| 28 | + randomNumber := rand.Intn(100) + 1 |
| 29 | + randomNumber2 := rand.Intn(100) + 1 |
| 30 | + piID := "a1a23e24-220d-4a20-a678-c8c5e84056bd" |
| 31 | + name := fmt.Sprintf("power-go-test-volume-%d", randomNumber) |
| 32 | + name2 := fmt.Sprintf("power-go-test-volume-%d", randomNumber2) |
| 33 | + size := 20.0 |
| 34 | + vtype := "tier3" |
| 35 | + sharable := true |
| 36 | + replicationEnabled := false |
| 37 | + |
| 38 | + // VM inputs |
| 39 | + serverName := fmt.Sprintf("power-go-test-%d", randomNumber) |
| 40 | + imageID := "cd447165-f45d-48f4-b07f-7c9a11ec84c7" |
| 41 | + networkID := "f324429e-5716-40c1-a01b-4d36ec8b82e3" |
| 42 | + memory := 4.0 |
| 43 | + processors := 2.0 |
| 44 | + procType := "shared" |
| 45 | + sysType := "s922" |
| 46 | + |
| 47 | + // authenticator := &core.BearerTokenAuthenticator{ |
| 48 | + // BearerToken: token, |
| 49 | + // } |
| 50 | + authenticator := &core.IamAuthenticator{ |
| 51 | + ApiKey: "PaBXQ7QI6AyBm86Bqo0C45o_6LmW9I76csCkWLojdZLQ", |
| 52 | + // Uncomment for test environment |
| 53 | + URL: "https://iam.test.cloud.ibm.com", |
| 54 | + } |
| 55 | + // Create the session |
| 56 | + options := &ps.IBMPIOptions{ |
| 57 | + Authenticator: authenticator, |
| 58 | + UserAccount: accountID, |
| 59 | + Zone: zone, |
| 60 | + URL: url, |
| 61 | + Debug: true, |
| 62 | + } |
| 63 | + session, err := ps.NewIBMPISession(options) |
| 64 | + if err != nil { |
| 65 | + log.Fatal(err) |
| 66 | + } |
| 67 | + powerClientVolume := v.NewIBMPIVolumeClient(context.Background(), session, piID) |
| 68 | + if err != nil { |
| 69 | + log.Fatal(err) |
| 70 | + } |
| 71 | + bodyVolume := &models.CreateDataVolume{ |
| 72 | + Name: &name, |
| 73 | + Size: &size, |
| 74 | + DiskType: vtype, |
| 75 | + Shareable: &sharable, |
| 76 | + ReplicationEnabled: &replicationEnabled, |
| 77 | + } |
| 78 | + bodyVolume2 := &models.CreateDataVolume{ |
| 79 | + Name: &name2, |
| 80 | + Size: &size, |
| 81 | + DiskType: vtype, |
| 82 | + Shareable: &sharable, |
| 83 | + ReplicationEnabled: &replicationEnabled, |
| 84 | + } |
| 85 | + log.Print("Creating first Volume\n") |
| 86 | + createRespOk, err := powerClientVolume.CreateVolume(bodyVolume) |
| 87 | + if err != nil { |
| 88 | + log.Fatal(err) |
| 89 | + } |
| 90 | + log.Printf("***************[1]****************** %+v\n", *createRespOk) |
| 91 | + |
| 92 | + volumeID := *createRespOk.VolumeID |
| 93 | + getResp, err := powerClientVolume.Get(volumeID) |
| 94 | + if err != nil { |
| 95 | + log.Fatal(err) |
| 96 | + } |
| 97 | + log.Printf("***************[2]****************** %+v \n", *getResp) |
| 98 | + log.Print("Creating Second Volume\n") |
| 99 | + createRespOk2, err := powerClientVolume.CreateVolume(bodyVolume2) |
| 100 | + if err != nil { |
| 101 | + log.Fatal(err) |
| 102 | + } |
| 103 | + log.Printf("***************[3]****************** %+v\n", *createRespOk2) |
| 104 | + |
| 105 | + volumeID2 := *createRespOk2.VolumeID |
| 106 | + getResp2, err := powerClientVolume.Get(volumeID) |
| 107 | + if err != nil { |
| 108 | + log.Fatal(err) |
| 109 | + } |
| 110 | + log.Printf("***************[4]****************** %+v \n", *getResp2) |
| 111 | + |
| 112 | + powerClientVM := v.NewIBMPIInstanceClient(context.Background(), session, piID) |
| 113 | + if err != nil { |
| 114 | + log.Fatal(err) |
| 115 | + } |
| 116 | + bodyVM := &models.PVMInstanceCreate{ |
| 117 | + ImageID: &imageID, |
| 118 | + NetworkIDs: []string{networkID}, |
| 119 | + ServerName: &serverName, |
| 120 | + VolumeIDs: []string{volumeID, volumeID2}, |
| 121 | + Memory: &memory, |
| 122 | + Processors: &processors, |
| 123 | + ProcType: &procType, |
| 124 | + SysType: sysType, |
| 125 | + } |
| 126 | + |
| 127 | + log.Print("Creating VM\n") |
| 128 | + createRespOkVM, err := powerClientVM.Create(bodyVM) |
| 129 | + if err != nil { |
| 130 | + log.Fatal(err) |
| 131 | + } |
| 132 | + if len(*createRespOkVM) == 0 { |
| 133 | + log.Fatal("create response is empty") |
| 134 | + } |
| 135 | + log.Printf("***************[5]****************** %+v\n", *createRespOk) |
| 136 | + |
| 137 | + insID := "" |
| 138 | + for _, in := range *createRespOkVM { |
| 139 | + insID = *in.PvmInstanceID |
| 140 | + } |
| 141 | + if insID == "" { |
| 142 | + log.Fatal("instance ID is empty") |
| 143 | + } |
| 144 | + |
| 145 | + getRespVM, err := powerClientVM.Get(insID) |
| 146 | + if err != nil { |
| 147 | + log.Fatal(err) |
| 148 | + } |
| 149 | + log.Printf("***************[6]****************** %+v \n", *getRespVM) |
| 150 | + waitForInstanceAvailable(insID, powerClientVM) |
| 151 | + log.Print("Detaching Volumes\n") |
| 152 | + detachBody := &models.VolumesDetach{ |
| 153 | + VolumeIDs: []string{volumeID, volumeID2}, |
| 154 | + } |
| 155 | + |
| 156 | + createRespOkDetach, err := powerClientVolume.BulkVolumeDetach(insID, detachBody) |
| 157 | + if err != nil { |
| 158 | + log.Fatal(err) |
| 159 | + } |
| 160 | + log.Printf("***************[7]****************** %+v\n", *createRespOkDetach) |
| 161 | + log.Print("Deleting VM\n") |
| 162 | + err = powerClientVM.Delete(insID) |
| 163 | + if err != nil { |
| 164 | + log.Fatal(err) |
| 165 | + } |
| 166 | + log.Print("Deleting First Volume\n") |
| 167 | + err = powerClientVolume.DeleteVolume(volumeID) |
| 168 | + if err != nil { |
| 169 | + log.Fatal(err) |
| 170 | + } |
| 171 | + log.Print("Deleting Second Volume\n") |
| 172 | + err = powerClientVolume.DeleteVolume(volumeID2) |
| 173 | + if err != nil { |
| 174 | + log.Fatal(err) |
| 175 | + } |
| 176 | + |
| 177 | +} |
| 178 | + |
| 179 | +func waitForInstanceAvailable(id string, powerClientVM *v.IBMPIInstanceClient) { |
| 180 | + for start := time.Now(); time.Since(start) < time.Minute*30; { |
| 181 | + time.Sleep(60 * time.Second) |
| 182 | + pvm, err := powerClientVM.Get(id) |
| 183 | + if err != nil { |
| 184 | + log.Fatal(err) |
| 185 | + } |
| 186 | + if *pvm.Status == helpers.PIInstanceAvailable && pvm.Health.Status == helpers.PIInstanceHealthOk { |
| 187 | + break |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments