github.com/fanux/shipyard@v0.0.0-20161009071005-6515ce223235/controller/plugin/client/api.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"log"
     8  	"net/http"
     9  	"strings"
    10  )
    11  
    12  type Api struct {
    13  	host string
    14  	port string
    15  }
    16  
    17  func (this Api) GetPluginInfo(pluginName string) (Plugin, error) {
    18  	//log.Printf("get plugin info:%s", pluginName)
    19  
    20  	p := new(Plugin)
    21  
    22  	/*
    23  		p := Plugin{
    24  			Name:        "plugin_pipeline",
    25  			Kind:        "",
    26  			Status:      "enable",
    27  			Description: "",
    28  			Spec:        "",
    29  			Manual:      "",
    30  		}
    31  	*/
    32  
    33  	client := &http.Client{}
    34  
    35  	url := fmt.Sprintf("http://%s%s/plugins/%s", this.host, this.port, pluginName)
    36  
    37  	req, _ := http.NewRequest("GET", url, nil)
    38  
    39  	resp, err := client.Do(req)
    40  	if err != nil {
    41  		log.Printf("get plugin info failed: %s", err)
    42  	}
    43  	defer resp.Body.Close()
    44  
    45  	body, err := ioutil.ReadAll(resp.Body)
    46  
    47  	json.Unmarshal(body, p)
    48  
    49  	log.Printf("get plugin info: %s", p.Description)
    50  
    51  	return *p, err
    52  }
    53  
    54  func (this Api) GetPluginStrategies(pluginName string) ([]Strategy, error) {
    55  	/*
    56  		log.Printf("get plugin strategies:%s", pluginName)
    57  		strategies := []Strategy{
    58  			{
    59  				PluginName: "plugin_pipeline",
    60  				Name:       "scale-by-hour",
    61  				Status:     "enable",
    62  				Document:   `[{"Cron":"1","Apps":[{"App":"ats","Number":20},{"App":"hadoop","Number":10}]}]`,
    63  			},
    64  		}
    65  	*/
    66  
    67  	strategies := []Strategy{}
    68  
    69  	client := &http.Client{}
    70  
    71  	url := fmt.Sprintf("http://%s%s/plugins/%s/strategies", this.host, this.port, pluginName)
    72  
    73  	req, _ := http.NewRequest("GET", url, nil)
    74  
    75  	resp, err := client.Do(req)
    76  	if err != nil {
    77  		log.Printf("get plugin strategies info failed: %s", err)
    78  	}
    79  	defer resp.Body.Close()
    80  
    81  	body, err := ioutil.ReadAll(resp.Body)
    82  
    83  	json.Unmarshal(body, &strategies)
    84  
    85  	return strategies, err
    86  }
    87  
    88  func (this Api) ScaleApps(appscale []AppScale) error {
    89  	s, _ := json.Marshal(appscale)
    90  	log.Printf("scale apps: \n%s\n", string(s))
    91  
    92  	client := &http.Client{}
    93  
    94  	url := fmt.Sprintf("http://%s%s/plugins/scale", this.host, this.port)
    95  
    96  	req, _ := http.NewRequest("POST", url, strings.NewReader(string(s)))
    97  
    98  	req.Header.Set("Content-type", "application/json")
    99  
   100  	resp, err := client.Do(req)
   101  	if err != nil {
   102  		log.Printf("get plugin strategies info failed: %s", err)
   103  		fmt.Println(resp)
   104  	}
   105  
   106  	return err
   107  }