Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Commit

Permalink
[#46] started reading rancher 1.6 API
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Pozzi committed Nov 10, 2019
1 parent 360f505 commit 1bde432
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,21 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>

<!-- rancher api -->
<!-- https://mvnrepository.com/artifact/br.com.basis/rancher-java-sdk -->
<dependency>
<groupId>br.com.basis</groupId>
<artifactId>rancher-java-sdk</artifactId>
<version>1.0.0</version>
</dependency>

<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.3.0</version>
</dependency>

</dependencies>

<build>
Expand Down
114 changes: 114 additions & 0 deletions src/main/java/de/bonndan/nivio/input/rancher1/APIWalker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package de.bonndan.nivio.input.rancher1;

import de.bonndan.nivio.ProcessingException;
import de.bonndan.nivio.input.dto.ItemDescription;
import de.bonndan.nivio.input.dto.SourceReference;
import io.rancher.Rancher;
import io.rancher.service.ProjectService;
import io.rancher.service.ServiceService;
import io.rancher.service.StackService;
import io.rancher.type.Project;
import io.rancher.type.Service;
import io.rancher.type.Stack;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;

import static de.bonndan.nivio.input.rancher1.ItemDescriptionFactoryRancher1API.API_ACCESS_KEY;
import static de.bonndan.nivio.input.rancher1.ItemDescriptionFactoryRancher1API.API_SECRET_KEY;

class APIWalker {

private Rancher rancher;
private SourceReference reference;

public APIWalker(SourceReference reference) {
Rancher.Config config = getConfig(reference);
this.rancher = new Rancher(config);
this.reference = reference;
}

public List<ItemDescription> getDescriptions() {

String projectName = (String) reference.getProperty("projectName");
Project project = getProject(projectName)
.orElseThrow(() -> new ProcessingException(reference.getLandscapeDescription(), "Project " + projectName + "not found"));
String accountId = project.getId();


Map<String, Stack> stacks = getStacksById(accountId);
List<Service> services = getServices(accountId);

return asDescriptions(services, stacks);
}

private List<Service> getServices(final String accountId) {
ServiceService service = rancher.type(ServiceService.class);
try {
return service.list().execute().body().getData().stream()
.filter(service1 -> service1.getAccountId().equals(accountId))
.collect(Collectors.toList());
} catch (IOException e) {
throw new ProcessingException(reference.getLandscapeDescription(), "Could not load services from Rancher API", e);
}
}

private Map<String, Stack> getStacksById(String accountId) {
StackService stackService = rancher.type(StackService.class);
Map<String, Stack> stacks = new HashMap<>();
try {
stackService.list().execute().body().getData().stream()
.filter(stack -> stack.getAccountId().equals(accountId))
.forEach(stack -> stacks.put(stack.getId(), stack));
return stacks;
} catch (IOException e) {
throw new ProcessingException(reference.getLandscapeDescription(), "Could not access Rancher API", e);
}
}

private Optional<Project> getProject(String projectName) {
ProjectService projectService = rancher.type(ProjectService.class);
List<Project> projects = null;
try {
projects = projectService.list().execute().body().getData();
} catch (IOException e) {
throw new ProcessingException("Could not load projects", e);
}
return projects.stream()
.filter(project -> StringUtils.isEmpty(projectName) || project.getName().equals(projectName))
.findFirst();
}

private List<ItemDescription> asDescriptions(List<Service> data, final Map<String, Stack> stacks) {
List<ItemDescription> descriptions = new ArrayList<>();

data.forEach(service -> {
ItemDescription item = new ItemDescription();
item.setIdentifier(service.getName());
Stack stack = stacks.get(service.getStackId());
item.setGroup(stack.getName());
descriptions.add(item);
});

return descriptions;
}


private Rancher.Config getConfig(SourceReference reference) {
Rancher.Config config = null;
try {
config = new Rancher.Config(
new URL(reference.getUrl()),
(String) reference.getProperty(API_ACCESS_KEY),
(String) reference.getProperty(API_SECRET_KEY)
);
} catch (MalformedURLException e) {
throw new ProcessingException(reference.getLandscapeDescription(), "Could not configure rancher API: " + e.getMessage(), e);
}
return config;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.bonndan.nivio.input.rancher1;

import de.bonndan.nivio.input.ItemDescriptionFactory;
import de.bonndan.nivio.input.dto.ItemDescription;
import de.bonndan.nivio.input.dto.SourceReference;
import org.springframework.stereotype.Service;

import java.net.URL;
import java.util.Arrays;
import java.util.List;

@Service
public class ItemDescriptionFactoryRancher1API implements ItemDescriptionFactory {

public static final String API_SECRET_KEY = "apiSecretKey";
public static final String API_ACCESS_KEY = "apiAccessKey";

@Override
public List<String> getFormats() {
return Arrays.asList("rancher1");
}

@Override
public List<ItemDescription> getDescriptions(SourceReference reference, URL baseUrl) {
APIWalker apiWalker = new APIWalker(reference);
return apiWalker.getDescriptions();
}

}
8 changes: 8 additions & 0 deletions src/test/resources/example/example_rancher_api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
identifier: rancher-api

sources:
- url: "http://rancher-server/v2-beta/"
projectName: Default
apiAccessKey: ${API_ACCESS_KEY}
apiSecretKey: ${API_SECRET_KEY}
format: rancher1

0 comments on commit 1bde432

Please sign in to comment.