net.nikore.gozer.marathon.MarathonHost.java Source code

Java tutorial

Introduction

Here is the source code for net.nikore.gozer.marathon.MarathonHost.java

Source

/**
 * Copyright (C) 2015 Matt Christiansen (matt@nikore.net)
 *
 * Licensed 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 net.nikore.gozer.marathon;

import java.net.InetAddress;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.Inject;

import io.netty.buffer.ByteBuf;
import io.reactivex.netty.protocol.http.client.HttpClientRequest;
import net.nikore.gozer.marathon.model.v2.App;
import net.nikore.gozer.router.RibbonUtils;
import net.nikore.gozer.router.SimpleRibbonResponse;

public class MarathonHost {
    private final RibbonUtils utils;
    private final ObjectMapper mapper;
    private final String ip;
    private final String marathonHost;

    @Inject
    public MarathonHost(RibbonUtils utils, ObjectMapper mapper, GozerMarathonConfig config) throws Exception {
        this.utils = utils;
        this.mapper = mapper;
        this.ip = InetAddress.getLocalHost().getHostAddress();
        this.marathonHost = config.getMarathonHost();
    }

    public List<App> getAllApps() throws Exception {
        HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("http://" + marathonHost + "/v2/apps");

        SimpleRibbonResponse response = utils.simpleRequest(request);

        Map<String, List<App>> apps = mapper.readValue(response.getContent(),
                new TypeReference<Map<String, List<App>>>() {
                });

        return apps.get("apps");
    }

    public App getApp(String id) throws Exception {
        HttpClientRequest<ByteBuf> request = HttpClientRequest
                .createGet("http://" + marathonHost + "/v2/apps/" + id);

        SimpleRibbonResponse response = utils.simpleRequest(request);

        Map<String, App> apps = mapper.readValue(response.getContent(), new TypeReference<Map<String, App>>() {
        });

        return apps.get("app");
    }

    public void registerCallback() throws Exception {
        String reqUrl = "http://" + marathonHost + "/v2/eventSubscriptions?callbackUrl=http://" + ip
                + "/marathon/callback";

        HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost(reqUrl);

        SimpleRibbonResponse response = utils.simpleRequest(request);

        if (response.getStatusCode() >= 500 || response.getStatusCode() >= 400) {
            throw new RuntimeException("Marathon host threw an error trying too register callback");
        }
    }

    public void deleteCallback() throws Exception {
        String reqUrl = "http://" + marathonHost + "/v2/eventSubscriptions?callbackUrl=http://" + ip
                + "/marathon/callback";

        HttpClientRequest<ByteBuf> request = HttpClientRequest.createDelete(reqUrl);

        utils.simpleRequest(request);
    }
}