Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.openstack4j.api.heat;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
import org.openstack4j.api.AbstractTest;
import org.openstack4j.model.heat.AdoptStackData;
import org.openstack4j.model.heat.Stack;
import org.openstack4j.openstack.heat.domain.HeatAdoptStackData;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;

/**
* Test cases for Heat Stack Services function
*
* @author Ales Kemr
*/
@Test(suiteName="heat/stacks", enabled = true)
public class StackServiceTests extends AbstractTest {

private static final String JSON_ABANDON = "/heat/abandon.json";
private static final String JSON_ADOPT = "/heat/adopt.json";

public void testAbandonStack() throws Exception {
respondWith(JSON_ABANDON);

AdoptStackData adoptStackData = osv3().heat().stacks().abandon("stack_123", "416c09e9-2022-4d43-854b-0292ddff3f5d");
takeRequest();

assertEquals(adoptStackData.getName(), "stack_123");
assertEquals(adoptStackData.getStatus(), "COMPLETE");
final Map<String, Object> portResource = adoptStackData.getResources().get("network_port");
assertEquals(portResource.get("type"), "OS::Neutron::Port");
}

public void testAdoptStack() throws Exception {
respondWith(JSON_ADOPT);

AdoptStackData adoptStackData = new ObjectMapper().readValue(getResource(JSON_ABANDON), HeatAdoptStackData.class);
Stack adoptedStack = osv3().heat().stacks().adopt(adoptStackData, new HashMap<String, String>(), false, 30L, null);
takeRequest();

assertEquals(adoptedStack.getId(), "79370050-6038-4ea2-baaa-3e4706d59e0e");
}

@Override
protected Service service() {
return Service.ORCHESTRATION;
}

}
55 changes: 55 additions & 0 deletions core-test/src/main/resources/heat/abandon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"files":{

},
"status":"COMPLETE",
"name":"stack_123",
"tags":null,
"stack_user_project_id":"44ed5c0be2384092b8e8b6444812db5d",
"environment":{
"event_sinks":[

],
"parameter_defaults":{

},
"parameters":{

},
"resource_registry":{
"resources":{

}
}
},
"template":{
"heat_template_version":"2016-04-08",
"description":"A heat test template",
"resources":{
"network_port":{
"type":"OS::Neutron::Port",
"properties":{
"network_id":"network123"
}
}
}
},
"action":"CREATE",
"project_id":"0dbd25a2a75347cf88a0a52638b8fff3",
"id":"416c09e9-2022-4d43-854b-0292ddff3f5d",
"resources":{
"network_port":{
"status":"COMPLETE",
"name":"network_port",
"resource_data":{

},
"resource_id":"1d0dccbb-0c99-48cd-b41b-abeca3ac6a42",
"action":"CREATE",
"type":"OS::Neutron::Port",
"metadata":{

}
}
}
}
11 changes: 11 additions & 0 deletions core-test/src/main/resources/heat/adopt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"stack":{
"id":"79370050-6038-4ea2-baaa-3e4706d59e0e",
"links":[
{
"href":"http://any.os.com:8004/v1/0dbd25a2a75347cf88a0a52638b8fff3/stacks/stack_123/79370050-6038-4ea2-baaa-3e4706d59e0e",
"rel":"self"
}
]
}
}
24 changes: 23 additions & 1 deletion core/src/main/java/org/openstack4j/api/heat/StackService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Map;

import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.heat.AdoptStackData;
import org.openstack4j.model.heat.Stack;
import org.openstack4j.model.heat.StackCreate;
import org.openstack4j.model.heat.StackUpdate;
Expand Down Expand Up @@ -100,5 +101,26 @@ Stack create(String name, String template, Map<String, String> parameters,
*/
ActionResponse delete(String stackName, String stackId);


/**
* Deletes a stack but leaves its resources intact, and returns data that describes the stack and its resources.
*
* @param stackName Name of {@link Stack}
* @param stackId Id of {@link Stack}
*
* @return <code>adopt_stack_data</code> element representing by {@link AdoptStackData}
*/
AdoptStackData abandon(String stackName, String stackId);

/**
* Creates a stack from existing resources.
*
* @param adoptStackData Structure {@link AdoptStackData}, representing existing resources
* @param parameters Map of parameters
* @param disableRollback Enable or disable rollback
* @param timeOutMins Timeout in minutes
* @param template Template in Json-Format or YAML format. It is optional, used just in case there will be new resources (not included in adoptStackData)
* @return
*/
Stack adopt(AdoptStackData adoptStackData, Map<String, String> parameters,
boolean disableRollback, Long timeOutMins, String template);
}
59 changes: 59 additions & 0 deletions core/src/main/java/org/openstack4j/model/heat/AdoptStackData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.openstack4j.model.heat;

import java.util.Map;
import org.openstack4j.model.ModelEntity;

/**
* This interface describes <code>adopt_stack_data</code> element. It is used
* for stack adoption and as a return value for stack abandoning. All getters
* map to the possible return values of
* <code> Delete /v1/{tenant_id}/stacks/{stack_name}/{stack_id}/abandon</code>
*
* @see https://developer.openstack.org/api-ref/orchestration/v1
*
* @author Ales Kemr
*/
public interface AdoptStackData extends ModelEntity {

/**
* Returns stack action, e.g. CREATE
*
* @return stack action
*/
String getAction();

/**
* Returns the id of the stack
*
* @return the id of the stack
*/
String getId();

/**
* Returns the name of the stack
*
* @return the name of the stack
*/
String getName();

/**
* Returns the status of the stack
*
* @return the status of the stack
*/
String getStatus();

/**
* Returns stack template as a map
*
* @return stack template as a map
*/
Map<String, Object> getTemplate();

/**
* Returns map of existing resources, to be adopted into the stack
*
* @return Map of existing resources to be adopted into the stack
*/
Map<String, Map<String, Object>> getResources();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package org.openstack4j.openstack.heat.domain;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.MoreObjects;
import java.util.Map;
import org.openstack4j.model.heat.AdoptStackData;

/**
* This class contains all elements required for the creation of <code>adopt_stack_data</code> element. It is used for stack adoption and as a return value for stack abandoning.
* It uses Jackson annotation for (de)serialization into JSON.
*
* @author Ales Kemr
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class HeatAdoptStackData implements AdoptStackData {
private static final long serialVersionUID = 1L;

@JsonProperty("action")
private String action;

@JsonProperty("id")
private String id;

@JsonProperty("name")
private String name;

@JsonProperty("status")
private String status;

@JsonProperty("template")
private Map<String,Object> template;

@JsonProperty("resources")
private Map<String, Map<String, Object>> resources;

@Override
public String getAction() {
return action;
}

@Override
public String getId() {
return id;
}

@Override
public String getName() {
return name;
}

@Override
public String getStatus() {
return status;
}

@Override
public Map<String,Object> getTemplate() {
return template;
}

@Override
public Map<String, Map<String, Object>> getResources() {
return resources;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).omitNullValues()
.add("id", id)
.add("name", name)
.add("status", status)
.add("resources", resources)
.toString();
}

public static HeatAdoptStackDataBuilder builder() {
return new HeatAdoptStackDataBuilder();
}

public static class HeatAdoptStackDataBuilder {

private HeatAdoptStackData model;

public HeatAdoptStackDataBuilder() {
this.model = new HeatAdoptStackData();
}

public HeatAdoptStackDataBuilder(HeatAdoptStackData model) {
this.model = model;
}

public HeatAdoptStackDataBuilder action(String action) {
this.model.action = action;
return this;
}

public HeatAdoptStackDataBuilder id(String id) {
this.model.id = id;
return this;
}

public HeatAdoptStackDataBuilder name(String name) {
this.model.name = name;
return this;
}

public HeatAdoptStackDataBuilder status(String status) {
this.model.status = status;
return this;
}

public HeatAdoptStackDataBuilder template(Map<String,Object> template) {
this.model.template = template;
return this;
}

public HeatAdoptStackDataBuilder resources(Map<String, Map<String, Object>> resources) {
this.model.resources = resources;
return this;
}

public HeatAdoptStackData build() {
return model;
}
}


}
Loading