~canonical-is/charms/trusty/collectd/trunk

« back to all changes in this revision

Viewing changes to deps/interface/interface-http/README.md

  • Committer: Haw Loeung
  • Date: 2016-06-29 00:15:11 UTC
  • Revision ID: haw.loeung@canonical.com-20160629001511-eegvqv8qhlkti4ot
[trivial] Ran 'charm build' to fix failed pip version in Xenial.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Overview
 
2
 
 
3
This interface layer implements the basic form of the `http` interface protocol,
 
4
which is used for things such as reverse-proxies, load-balanced servers, REST
 
5
service discovery, et cetera.
 
6
 
 
7
# Usage
 
8
 
 
9
## Provides
 
10
 
 
11
By providing the `http` interface, your charm is providing an HTTP server that
 
12
can be load-balanced, reverse-proxied, used as a REST endpoint, etc.
 
13
 
 
14
Your charm need only provide the port on which it is serving its content, as
 
15
soon as the `{relation_name}.available` state is set:
 
16
 
 
17
```python
 
18
@when('website.available')
 
19
def configure_website(website):
 
20
    website.configure(port=hookenv.config('port'))
 
21
```
 
22
 
 
23
## Requires
 
24
 
 
25
By requiring the `http` interface, your charm is consuming one or more HTTP
 
26
servers, as a REST endpoint, to load-balance a set of servers, etc.
 
27
 
 
28
Your charm should respond to the `{relation_name}.available` state, which
 
29
indicates that there is at least one HTTP server connected.
 
30
 
 
31
The `services()` method returns a list of available HTTP services and their
 
32
associated hosts and ports.
 
33
 
 
34
The return value is a list of dicts of the following form:
 
35
 
 
36
```python
 
37
[
 
38
    {
 
39
        'service_name': name_of_service,
 
40
        'hosts': [
 
41
            {
 
42
                'hostname': address_of_host,
 
43
                'port': port_for_host,
 
44
            },
 
45
            # ...
 
46
        ],
 
47
    },
 
48
    # ...
 
49
]
 
50
```
 
51
 
 
52
A trivial example of handling this interface would be:
 
53
 
 
54
```python
 
55
from charms.reactive.helpers import data_changed
 
56
 
 
57
@when('reverseproxy.available')
 
58
def update_reverse_proxy_config(reverseproxy):
 
59
    services = reverseproxy.services()
 
60
    if not data_changed('reverseproxy.services', services):
 
61
        return
 
62
    for service in services:
 
63
        for host in service['hosts']:
 
64
            hookenv.log('{} has a unit {}:{}'.format(
 
65
                services['service_name'],
 
66
                host['hostname'],
 
67
                host['port']))
 
68
```