aboutsummaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/chef/ChefMock.java
blob: 1b2dad34b8d461374b76465b12df95caa0b01f3e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.chef;

import com.yahoo.vespa.hosted.controller.api.integration.chef.rest.ChefEnvironment;
import com.yahoo.vespa.hosted.controller.api.integration.chef.rest.ChefNode;
import com.yahoo.vespa.hosted.controller.api.integration.chef.rest.ChefResource;
import com.yahoo.vespa.hosted.controller.api.integration.chef.rest.Client;
import com.yahoo.vespa.hosted.controller.api.integration.chef.rest.CookBook;
import com.yahoo.vespa.hosted.controller.api.integration.chef.rest.NodeResult;
import com.yahoo.vespa.hosted.controller.api.integration.chef.rest.PartialNode;
import com.yahoo.vespa.hosted.controller.api.integration.chef.rest.PartialNodeResult;

import javax.ws.rs.NotFoundException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author mpolden
 */
public class ChefMock implements Chef {

    private final NodeResult result;
    private final List<String> chefEnvironments;

    public ChefMock() {
        result = new NodeResult();
        result.rows = new ArrayList<>();
        chefEnvironments = new ArrayList<>();
        chefEnvironments.add("hosted-verified-prod");
        chefEnvironments.add("hosted-infra-cd");
    }

    @Override
    public ChefResource getApi() {
        return null;
    }

    @Override
    public ChefNode getNode(String name) {
        return null;
    }

    @Override
    public Client getClient(String name) {
        return null;
    }

    @Override
    public ChefNode deleteNode(String name) {
        return null;
    }

    @Override
    public Client deleteClient(String name) {
        return null;
    }

    public void addSearchResult(ChefNode node) {
        result.rows.add(node);
    }

    @Override
    public NodeResult searchNodeByFQDN(String fqdn) {
        return result;
    }

    @Override
    public NodeResult searchNodes(String query) {
        return result;
    }

    @Override
    public PartialNodeResult partialSearchNodes(String query, List<AttributeMapping> returnAttributes) {
        PartialNodeResult partialNodeResult = new PartialNodeResult();
        partialNodeResult.rows = result.rows.stream()
                .map(chefNode -> {
                    Map<String, String> data = new HashMap<>();
                    data.put("fqdn", chefNode.name);
                    return new PartialNode(data);
                })
                .collect(Collectors.toList());
        return partialNodeResult;
    }

    @Override
    public void copyChefEnvironment(String fromEnvironmentName, String toEnvironmentName) {
        if(!chefEnvironments.contains(fromEnvironmentName)) {
            throw new NotFoundException(String.format("Source chef environment %s does not exist", fromEnvironmentName));
        }
        chefEnvironments.add(toEnvironmentName);
    }

    @Override
    public ChefEnvironment getChefEnvironment(String environmentName) {
        return null;
    }

    @Override
    public CookBook getCookbook(String cookbookName, String cookbookVersion) {
        return null;
    }

    @Override
    public String downloadResource(URL resourceURL) {
        return "";
    }
}