Skip to content

Commit

Permalink
Add network interfaces in virtual machine job result
Browse files Browse the repository at this point in the history
  • Loading branch information
tomskikh committed Sep 25, 2018
1 parent 5483ef7 commit 83ba8f8
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.bwsw.cloudstack.entities.events.jobresults

import java.util.UUID

import com.bwsw.cloudstack.entities.common.CommonJsonFormats.uuidJsonFormat
import spray.json.DefaultJsonProtocol._
import spray.json.RootJsonFormat

final case class NetworkInterface(id: UUID,
secondaryIps: Seq[SecondaryIp])


object NetworkInterface {

implicit val networkInterfaceCardJsonFormat: RootJsonFormat[NetworkInterface] =
jsonFormat(
NetworkInterface.apply,
"id",
"secondaryip"
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.bwsw.cloudstack.entities.events.jobresults

import java.util.UUID

import com.bwsw.cloudstack.entities.common.CommonJsonFormats.uuidJsonFormat
import spray.json.DefaultJsonProtocol._
import spray.json.RootJsonFormat

final case class SecondaryIp(id: UUID,
ipAddress: String)


object SecondaryIp {

implicit val secondaryIpJsonFormat: RootJsonFormat[SecondaryIp] =
jsonFormat(
SecondaryIp.apply,
"id",
"ipaddress"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@ import com.bwsw.cloudstack.entities.common.CommonJsonFormats.uuidJsonFormat
import spray.json.DefaultJsonProtocol._
import spray.json.RootJsonFormat

final case class VirtualMachineCreateJobResult(id: UUID,
account: String,
domainId: UUID,
serviceOfferingId: UUID)
final case class VirtualMachineJobResult(id: UUID,
account: String,
domainId: UUID,
serviceOfferingId: UUID,
networkInterfaces: Seq[NetworkInterface])


object VirtualMachineCreateJobResult {
object VirtualMachineJobResult {

implicit val virtualMachineCreateJobResultJsonFormat: RootJsonFormat[VirtualMachineCreateJobResult] =
implicit val virtualMachineCreateJobResultJsonFormat: RootJsonFormat[VirtualMachineJobResult] =
jsonFormat(
VirtualMachineCreateJobResult.apply,
VirtualMachineJobResult.apply,
"id",
"account",
"domainid",
"serviceofferingid"
"serviceofferingid",
"nic"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ package com.bwsw.cloudstack.entities.events.vm
import java.time.OffsetDateTime

import com.bwsw.cloudstack.entities.common.CommonJsonFormats._
import com.bwsw.cloudstack.entities.events.jobresults.VirtualMachineCreateJobResult
import com.bwsw.cloudstack.entities.events.jobresults.VirtualMachineJobResult
import com.bwsw.cloudstack.entities.events.{CloudStackEvent, EventDateTime, JobResult}
import spray.json.DefaultJsonProtocol._
import spray.json.RootJsonFormat

final case class VirtualMachineCreateEvent(status: Option[String],
eventDateTime: Option[OffsetDateTime],
jobResult: Option[JobResult[VirtualMachineCreateJobResult]])
jobResult: Option[JobResult[VirtualMachineJobResult]])
extends CloudStackEvent with EventDateTime


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import java.util.UUID

import com.bwsw.cloudstack.entities.events.Constants.Statuses
import com.bwsw.cloudstack.entities.events.account.{AccountCreateEvent, AccountDeleteEvent}
import com.bwsw.cloudstack.entities.events.jobresults.{NetworkInterface, VirtualMachineJobResult}
import com.bwsw.cloudstack.entities.events.user.UserCreateEvent
import com.bwsw.cloudstack.entities.events.vm.VirtualMachineDestroyEvent
import com.bwsw.cloudstack.entities.events.{CloudStackEvent, UnknownEvent}
import com.bwsw.cloudstack.entities.events.vm.{VirtualMachineCreateEvent, VirtualMachineDestroyEvent}
import com.bwsw.cloudstack.entities.events.{CloudStackEvent, JobResult, UnknownEvent}
import org.scalatest.{FlatSpec, Matchers}
import spray.json._

Expand Down Expand Up @@ -116,6 +117,56 @@ class DefaultJsonFormatsTests
}


it should "deserialize VirtualMachineCreateEvent properly" in {
val jsonString =
"""{
| "jobId": "2c9bd9c7-11e1-4354-abb5-2c619fbb4589",
| "commandEventType": "VM.CREATE",
| "processStatus": "0",
| "cmdInfo": "{\"iptonetworklist[0].networkid\":\"95b8b9cf-fd93-4601-b037-915b2bf5d5a2\",\"httpmethod\":\"GET\",\"templateid\":\"9b5a23d5-f503-11e7-a402-0242ac110003\",\"ctxAccountId\":\"37\",\"uuid\":\"991558d9-5dfe-4272-85aa-98cbf7c10830\",\"cmdEventType\":\"VM.CREATE\",\"serviceofferingid\":\"891b5344-83c6-453a-8521-1cdfa17f920b\",\"response\":\"json\",\"ctxUserId\":\"37\",\"hypervisor\":\"Simulator\",\"iptonetworklist[1].networkid\":\"a72d1fb8-c754-42f2-8bad-295084c4f981\",\"zoneid\":\"9f5c2d2a-05ac-476d-b274-f58cc211b007\",\"ctxStartEventId\":\"1873\",\"id\":\"36\",\"ctxDetails\":\"{\\\"interface com.cloud.vm.VirtualMachine\\\":\\\"991558d9-5dfe-4272-85aa-98cbf7c10830\\\",\\\"interface com.cloud.template.VirtualMachineTemplate\\\":\\\"9b5a23d5-f503-11e7-a402-0242ac110003\\\",\\\"interface com.cloud.dc.DataCenter\\\":\\\"9f5c2d2a-05ac-476d-b274-f58cc211b007\\\",\\\"interface com.cloud.offering.ServiceOffering\\\":\\\"891b5344-83c6-453a-8521-1cdfa17f920b\\\"}\",\"_\":\"1537859651513\"}",
| "instanceType": "VirtualMachine",
| "jobResult": "org.apache.cloudstack.api.response.UserVmResponse/virtualmachine/{\"id\":\"991558d9-5dfe-4272-85aa-98cbf7c10830\",\"name\":\"VM-991558d9-5dfe-4272-85aa-98cbf7c10830\",\"displayname\":\"VM-991558d9-5dfe-4272-85aa-98cbf7c10830\",\"account\":\"aaa\",\"userid\":\"9e476079-af42-4a55-9880-e9811595a104\",\"username\":\"aaa\",\"domainid\":\"bcaf64de-56ff-44ef-9f4a-d5959cb3d63b\",\"domain\":\"aaa\",\"created\":\"2018-09-25T07:14:11+0000\",\"state\":\"Running\",\"haenable\":false,\"zoneid\":\"9f5c2d2a-05ac-476d-b274-f58cc211b007\",\"zonename\":\"Sandbox-simulator-advanced\",\"templateid\":\"9b5a23d5-f503-11e7-a402-0242ac110003\",\"templatename\":\"CentOS 5.6 (64-bit) no GUI (Simulator)\",\"templatedisplaytext\":\"CentOS 5.6 (64-bit) no GUI (Simulator)\",\"passwordenabled\":false,\"serviceofferingid\":\"891b5344-83c6-453a-8521-1cdfa17f920b\",\"serviceofferingname\":\"Small Instance\",\"cpunumber\":1,\"cpuspeed\":500,\"memory\":512,\"guestosid\":\"88acb71a-f503-11e7-a402-0242ac110003\",\"rootdeviceid\":0,\"rootdevicetype\":\"ROOT\",\"securitygroup\":[],\"nic\":[{\"id\":\"674aeb81-c153-4b39-b462-85d47c304efc\",\"networkid\":\"95b8b9cf-fd93-4601-b037-915b2bf5d5a2\",\"networkname\":\"ggg\",\"netmask\":\"255.255.255.0\",\"gateway\":\"10.1.1.1\",\"ipaddress\":\"10.1.1.19\",\"isolationuri\":\"vlan://103\",\"broadcasturi\":\"vlan://103\",\"traffictype\":\"Guest\",\"type\":\"Isolated\",\"isdefault\":true,\"macaddress\":\"02:00:0f:71:00:01\",\"secondaryip\":[]},{\"id\":\"ace7cb25-6b7a-4315-b519-59d1fde5666f\",\"networkid\":\"a72d1fb8-c754-42f2-8bad-295084c4f981\",\"networkname\":\"hhhh\",\"netmask\":\"255.255.255.0\",\"gateway\":\"10.1.1.1\",\"ipaddress\":\"10.1.1.109\",\"isolationuri\":\"vlan://197\",\"broadcasturi\":\"vlan://197\",\"traffictype\":\"Guest\",\"type\":\"Isolated\",\"isdefault\":false,\"macaddress\":\"02:00:45:47:00:01\",\"secondaryip\":[]}],\"hypervisor\":\"Simulator\",\"details\":{},\"affinitygroup\":[],\"displayvm\":true,\"isdynamicallyscalable\":false,\"ostypeid\":142,\"tags\":[],\"jobid\":\"2c9bd9c7-11e1-4354-abb5-2c619fbb4589\",\"jobstatus\":0}",
| "resultCode": "0",
| "instanceUuid": "991558d9-5dfe-4272-85aa-98cbf7c10830",
| "user": "9e476079-af42-4a55-9880-e9811595a104",
| "command": "org.apache.cloudstack.api.command.user.vm.DeployVMCmd",
| "account": "67d724b9-d104-4f5c-ba1c-a2aa3d39632f",
| "status": "SUCCEEDED"
|}
""".stripMargin

val expectedEvent = VirtualMachineCreateEvent(
status = Some(Statuses.SUCCEEDED),
eventDateTime = None, //scalastyle:ignore
jobResult = Some(
JobResult(
path = "org.apache.cloudstack.api.response.UserVmResponse/virtualmachine/",
result = Right(
VirtualMachineJobResult(
id = UUID.fromString("991558d9-5dfe-4272-85aa-98cbf7c10830"),
account = "aaa",
domainId = UUID.fromString("bcaf64de-56ff-44ef-9f4a-d5959cb3d63b"),
serviceOfferingId = UUID.fromString("891b5344-83c6-453a-8521-1cdfa17f920b"),
networkInterfaces = Seq(
NetworkInterface(
id = UUID.fromString("674aeb81-c153-4b39-b462-85d47c304efc"),
secondaryIps = Seq.empty
),
NetworkInterface(
id = UUID.fromString("ace7cb25-6b7a-4315-b519-59d1fde5666f"),
secondaryIps = Seq.empty
)
)
)
)
)
)
)

jsonString.parseJson.convertTo[CloudStackEvent] shouldBe expectedEvent
}


it should "deserialize VirtualMachineDestroyEvent with field 'event' properly" in {
val jsonString =
"""{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import java.time.{OffsetDateTime, ZoneOffset}
import java.util.UUID

import com.bwsw.cloudstack.entities.events.Constants.Statuses
import com.bwsw.cloudstack.entities.events.jobresults.VirtualMachineCreateJobResult
import com.bwsw.cloudstack.entities.events.jobresults.VirtualMachineJobResult
import com.bwsw.cloudstack.entities.events.vm.VirtualMachineCreateEvent
import com.bwsw.cloudstack.entities.events.{CloudStackEvent, JobResult, UnknownEvent}
import org.scalatest.prop.TableDrivenPropertyChecks
Expand Down Expand Up @@ -98,16 +98,20 @@ class JsonFormatsTests
| "entity": "com.cloud.vm.VirtualMachine",
| "status": "Scheduled",
| "VirtualMachine": "63f55cf9-2d1b-42f0-9202-a784af1d39ed",
| "jobResult": "org.apache.cloudstack.api.response.UserVmResponse/virtualmachine/{\"id\":\"63f55cf9-2d1b-42f0-9202-a784af1d39ed\",\"account\":\"Account Name\",\"domainid\":\"16d7977e-43fd-47ad-9fff-6ef8d463516f\",\"serviceofferingid\":\"b1196c0e-0c1a-4416-bea8-f6a62309fac5\"}"
| "jobResult": "org.apache.cloudstack.api.response.UserVmResponse/virtualmachine/{\"id\":\"63f55cf9-2d1b-42f0-9202-a784af1d39ed\",\"account\":\"Account Name\",\"domainid\":\"16d7977e-43fd-47ad-9fff-6ef8d463516f\",\"serviceofferingid\":\"b1196c0e-0c1a-4416-bea8-f6a62309fac5\",\"nic\":[]}"
|}""".stripMargin,
VirtualMachineCreateEvent(
status = Some(Statuses.SCHEDULED),
eventDateTime = Some(OffsetDateTime.of(2017, 11, 12, 13, 36, 23, 0, ZoneOffset.ofHours(7))), //scalastyle:ignore
jobResult = Some(JobResult("org.apache.cloudstack.api.response.UserVmResponse/virtualmachine/",
Right(VirtualMachineCreateJobResult(UUID.fromString("63f55cf9-2d1b-42f0-9202-a784af1d39ed"),
"Account Name",
UUID.fromString("16d7977e-43fd-47ad-9fff-6ef8d463516f"),
UUID.fromString("b1196c0e-0c1a-4416-bea8-f6a62309fac5")))))
Right(
VirtualMachineJobResult(
id = UUID.fromString("63f55cf9-2d1b-42f0-9202-a784af1d39ed"),
account = "Account Name",
domainId = UUID.fromString("16d7977e-43fd-47ad-9fff-6ef8d463516f"),
serviceOfferingId = UUID.fromString("b1196c0e-0c1a-4416-bea8-f6a62309fac5"),
networkInterfaces = Seq.empty
))))
)
),
("""{"event": "custom"}""", CustomEvent),
Expand Down

0 comments on commit 83ba8f8

Please sign in to comment.