This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#46] started reading rancher 1.6 API
- Loading branch information
Daniel Pozzi
committed
Nov 10, 2019
1 parent
360f505
commit 1bde432
Showing
4 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/main/java/de/bonndan/nivio/input/rancher1/APIWalker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/de/bonndan/nivio/input/rancher1/ItemDescriptionFactoryRancher1API.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |