golang ( “net/http“包实例运用)

package main

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
_ "net/http"
)

func main(){

	Authorization  := "################"   //手动更改自己的token
	idPipe := []string{
		"##############", "############",


	}

	i:=0
	for i<=3 {
		pipelineId:=idPipe[i]
		projectId:=idPipe[i+1]
		r1:=cdprespose1{
			PipelineID:      pipelineId,
			ProjectID:       projectId,

		}
		r2:=cdpresponse2{

		}
		urlR1 := "########" + pipelineId + "&projectId=" + projectId
		urlR2 := "#########" + pipelineId
		request1, err := http.NewRequest("GET", urlR1, nil)
		request1.Header.Set("Authorization", Authorization)
		request1.Header.Set("Content-Type", "application/json;charset=UTF-8")
		request1.Header.Set("projectId", projectId)
		request1.Header.Set("Host", "cdp.cestc.cn:8888")
		resp1, err := http.DefaultClient.Do(request1)
		if err != nil {
			return
		}
		by1, err := ioutil.ReadAll(resp1.Body)        body 流用ioutil.ReadAll接收
		json.Unmarshal(by1, &r1)

		request2, err := http.NewRequest("GET", urlR2, nil)
		request2.Header.Set("Authorization", Authorization)
		request2.Header.Set("Content-Type", "application/json;charset=UTF-8")
		request2.Header.Set("projectId", projectId)
		request2.Header.Set("Host", "cdp.cestc.cn:8888")
		resp2, err := http.DefaultClient.Do(request2)
		if err != nil {
			return
		}
		by2, err := ioutil.ReadAll(resp2.Body)
		if err != nil {
			return
		}
		json.Unmarshal(by2, &r2)
		if err != nil {
			return
		}

		r3 := CdpRequest{}
		r3.PipelineID = r1.PipelineID
		r3.Applications = []struct {
			ID string `json:"id"`
		}{
			{
				ID: r1.ApplicationID,
			},
		}

		r3.Stages = append(r3.Stages, r2.Data...)       //拼接struct 
		if err != nil {
			log.Printf("error:", err.Error())
			return
		}
		for h := range r3.Stages {
			for j := range r3.Stages[h].Groups {
				for k := range r3.Stages[h].Groups[j].Tasks {
					r3.Stages[h].Groups[j].Tasks[k].Options["DOCKER_IMAGE_NAME"] = "yyyyyy"
				}
			}
		}
		jsonstu1, err := json.Marshal(&r3)            //json 序列化
		if err != nil {
			log.Printf("error:", err.Error())
			return
		}
		fmt.Println(string(jsonstu1))
		if err != nil {
			log.Printf("error:", err.Error())
			return
		}
		resp3, err := http.NewRequest("POST", "http://cdp.cestc.cn:8888/api/v1/pipeline/pipeline/audit/save", bytes.NewBuffer(jsonstu1))
		if err != nil {
			return
		}
		resp3.Header.Set("Authorization", Authorization)
		resp3.Header.Set("Content-Type", "application/json;charset=UTF-8")
		resp3.Header.Set("projectId", projectId)
		resp3.Header.Set("Host", "cdp.cestc.cn:8888")
		client := &http.Client{}
		response, error := client.Do(resp3)
		if error != nil {
			log.Printf("error:\n", error.Error())
			return
		}
		fmt.Println("response Status:", response.Status)
		str1, err := ioutil.ReadAll(response.Body)
		if err != nil {
			log.Printf("error:", err.Error())
			return
		}
		fmt.Println(string(str1))
		i+=2

	}
}
type cdprespose1 struct {
	PipelineID      string      `json:"pipelineId"`
	ProjectID       string      `json:"projectId"`
	ApplicationID   string      `json:"applicationId"`
	ApplicationName string      `json:"applicationName"`
	CodeRepoID      string      `json:"codeRepoId"`
	PipelineName    string      `json:"pipelineName"`
	PipelineDesc    string      `json:"pipelineDesc"`
	PipelineFrom    int         `json:"pipelineFrom"`
	PipelineBizType int         `json:"pipelineBizType"`
	Enable          bool        `json:"enable"`
	BuildClusterID  interface{} `json:"buildClusterId"`
	BuildTimeout    int         `json:"buildTimeout"`
}
type cdpresponse2  struct {
	Code    int    `json:"code"`
	Desc    string `json:"desc"`
	Message string `json:"message"`
	Data    []struct {
		Name        string      `json:"name"`
		Description interface{} `json:"description"`
		Enable      bool        `json:"enable"`
		Key         string      `json:"key"`
		Toolbar     bool        `json:"toolbar"`
		Groups      []struct {
			Name        string      `json:"name"`
			Description interface{} `json:"description"`
			Enable      bool        `json:"enable"`
			Key         string      `json:"key"`
			Toolbar     bool        `json:"toolbar"`
			Tasks       []struct {
				Name        string      `json:"name"`
				Description string      `json:"description"`
				Type        string      `json:"type"`
				Application string      `json:"application"`
				Key         string      `json:"key"`
				Toolbar     bool        `json:"toolbar"`
				Image       interface{} `json:"image"`
				Script      interface{} `json:"script"`
				Options     map[string]string `json:"options"`
				Params struct {
				} `json:"params"`
				Reports    interface{} `json:"reports"`
				Enable     bool        `json:"enable"`
				Retry      int         `json:"retry"`
				Timeout    int         `json:"timeout"`
				Privileged bool        `json:"privileged"`
			} `json:"tasks"`
		} `json:"groups"`
	} `json:"data"`
	Success bool `json:"success"`
}

type CdpRequest  struct {
	PipelineID   string `json:"pipelineId"`
	Applications []struct {
		ID string `json:"id"`
	} `json:"applications"`
	Stages []struct {
		Name        string      `json:"name"`
		Description interface{} `json:"description"`
		Enable      bool        `json:"enable"`
		Key         string      `json:"key"`
		Toolbar     bool        `json:"toolbar"`
		Groups      []struct {
			Name        string      `json:"name"`
			Description interface{} `json:"description"`
			Enable      bool        `json:"enable"`
			Key         string      `json:"key"`
			Toolbar     bool        `json:"toolbar"`
			Tasks       []struct {
				Name        string      `json:"name"`
				Description string      `json:"description"`
				Type        string      `json:"type"`
				Application string      `json:"application"`
				Key         string      `json:"key"`
				Toolbar     bool        `json:"toolbar"`
				Image       interface{} `json:"image"`
				Script      interface{} `json:"script"`
				Options    map[string]string `json:"options"`
				Params struct {
				} `json:"params"`
				Reports    interface{} `json:"reports"`
				Enable     bool        `json:"enable"`
				Retry      int         `json:"retry"`
				Timeout    int         `json:"timeout"`
				Privileged bool        `json:"privileged"`
			} `json:"tasks"`
		} `json:"groups"`
	} `json:"stages"`
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值