/*
Copyright (c) 2016 Red Hat, Inc.

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 org.ovirt.engine.sdk4.examples;

import static org.ovirt.engine.sdk4.ConnectionBuilder.connection;

import org.ovirt.engine.sdk4.Connection;
import org.ovirt.engine.sdk4.services.HostService;
import org.ovirt.engine.sdk4.services.HostsService;
import org.ovirt.engine.sdk4.types.Host;
import org.ovirt.engine.sdk4.types.HostStatus;

// This example will connect to the server, search for a host by name and remove it:
public class RemoveHost {
    public static void main(String[] args) throws Exception {
        // Create the connection to the server:
        Connection connection = connection()
            .url("https://engine40.example.com/ovirt-engine/api")
            .user("admin@internal")
            .password("redhat123")
            .trustStoreFile("truststore.jks")
            .build();

        // Find the service that manages hosts:
        HostsService hostsService = connection.systemService().hostsService();

        // Find the host:
        Host host = hostsService.list()
            .search("name=myhost")
            .send()
            .hosts()
            .get(0);

        // Find the service that manages the host:
        HostService hostService = hostsService.hostService(host.id());

        // If the host isn't down or in maintenance then move it to maintenance:
        if (host.status() != HostStatus.MAINTENANCE) {
            hostService.deactivate().send();
        }

        // Remove the host:
        hostService.remove().send();

        // Close the connection to the server:
        connection.close();
    }
}
