import vm
import openstack
import ssh

#define the cluster object
#it is responsible for connecting to OpenStack
entity MyCluster:
#inputs: the required parameters
    string connection_url
    string username
    string password
    string tenant
    string network_name
    string image_id 
    string flavor
end

#input: the ssh key for all VMs
ssh::Key key [1] -- [0:] MyCluster clusters

#input: the OS for all VMs
std::OS os [1] -- [0:] MyCluster clusters

#internal: objects needed to construct hosts
vm::IaaS iaas [1] -- [0:] MyCluster clusters
vm::Network network [1] -- [0:] MyCluster clusters

implementation connection for MyCluster:
    #connect to OpenStack
    provider = openstack::OpenstackConfig(
        connection_url=connection_url, 
        username=username,
        password=password,
        tenant=tenant)  
    #get the iaas object
    self.iaas = vm::IaaS(name = "iaas_openstack", route_script = "", config = provider)
    #get the network
    self.network=vm::Network(name=network_name)
end

implement MyCluster using connection

#define our own host type
entity MyHost extends vm::Host:
end

#input: the cluster object
MyCluster cluster [1] -- [0:] MyHost hosts

implementation myhost for MyHost:
    #wire up all config for agent injection
    env_name = std::environment_name()
    env_id = std::environment()
    env_server = std::environment_server()
    port = 8888

    #wire up all config for vm creation
    self.image = cluster.image_id
    self.network =  cluster.network
    self.user_data = std::template("mymodule/user_data.tmpl")
    self.public_key = cluster.key
    self.os = cluster.os
    self.flavor = cluster.flavor
    self.iaas = cluster.iaas
end

# use our implemenation
# and also the catchall std::hostDefaults
# and vm::ipFact which will discover the ip for this host using the OpenStack API's
implement MyHost using myhost, std::hostDefaults, vm::ipFact
