log_project.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /*
  2. Package sls implements the SDK(v0.5.0) of Simple Log Service(abbr. SLS).
  3. For more description about SLS, please read this article:
  4. http://gitlab.alibaba-inc.com/sls/doc.
  5. */
  6. package alils
  7. import (
  8. "encoding/json"
  9. "fmt"
  10. "io/ioutil"
  11. "net/http"
  12. "net/http/httputil"
  13. )
  14. // Error message in SLS HTTP response.
  15. type errorMessage struct {
  16. Code string `json:"errorCode"`
  17. Message string `json:"errorMessage"`
  18. }
  19. type LogProject struct {
  20. Name string // Project name
  21. Endpoint string // IP or hostname of SLS endpoint
  22. AccessKeyId string
  23. AccessKeySecret string
  24. }
  25. // NewLogProject creates a new SLS project.
  26. func NewLogProject(name, endpoint, accessKeyId, accessKeySecret string) (p *LogProject, err error) {
  27. p = &LogProject{
  28. Name: name,
  29. Endpoint: endpoint,
  30. AccessKeyId: accessKeyId,
  31. AccessKeySecret: accessKeySecret,
  32. }
  33. return p, nil
  34. }
  35. // ListLogStore returns all logstore names of project p.
  36. func (p *LogProject) ListLogStore() (storeNames []string, err error) {
  37. h := map[string]string{
  38. "x-sls-bodyrawsize": "0",
  39. }
  40. uri := fmt.Sprintf("/logstores")
  41. r, err := request(p, "GET", uri, h, nil)
  42. if err != nil {
  43. return
  44. }
  45. buf, err := ioutil.ReadAll(r.Body)
  46. if err != nil {
  47. return
  48. }
  49. if r.StatusCode != http.StatusOK {
  50. errMsg := &errorMessage{}
  51. err = json.Unmarshal(buf, errMsg)
  52. if err != nil {
  53. err = fmt.Errorf("failed to list logstore")
  54. dump, _ := httputil.DumpResponse(r, true)
  55. fmt.Printf("%s\n", dump)
  56. return
  57. }
  58. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  59. return
  60. }
  61. type Body struct {
  62. Count int
  63. LogStores []string
  64. }
  65. body := &Body{}
  66. err = json.Unmarshal(buf, body)
  67. if err != nil {
  68. return
  69. }
  70. storeNames = body.LogStores
  71. return
  72. }
  73. // GetLogStore returns logstore according by logstore name.
  74. func (p *LogProject) GetLogStore(name string) (s *LogStore, err error) {
  75. h := map[string]string{
  76. "x-sls-bodyrawsize": "0",
  77. }
  78. r, err := request(p, "GET", "/logstores/"+name, h, nil)
  79. if err != nil {
  80. return
  81. }
  82. buf, err := ioutil.ReadAll(r.Body)
  83. if err != nil {
  84. return
  85. }
  86. if r.StatusCode != http.StatusOK {
  87. errMsg := &errorMessage{}
  88. err = json.Unmarshal(buf, errMsg)
  89. if err != nil {
  90. err = fmt.Errorf("failed to get logstore")
  91. dump, _ := httputil.DumpResponse(r, true)
  92. fmt.Printf("%s\n", dump)
  93. return
  94. }
  95. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  96. return
  97. }
  98. s = &LogStore{}
  99. err = json.Unmarshal(buf, s)
  100. if err != nil {
  101. return
  102. }
  103. s.project = p
  104. return
  105. }
  106. // CreateLogStore creates a new logstore in SLS,
  107. // where name is logstore name,
  108. // and ttl is time-to-live(in day) of logs,
  109. // and shardCnt is the number of shards.
  110. func (p *LogProject) CreateLogStore(name string, ttl, shardCnt int) (err error) {
  111. type Body struct {
  112. Name string `json:"logstoreName"`
  113. TTL int `json:"ttl"`
  114. ShardCount int `json:"shardCount"`
  115. }
  116. store := &Body{
  117. Name: name,
  118. TTL: ttl,
  119. ShardCount: shardCnt,
  120. }
  121. body, err := json.Marshal(store)
  122. if err != nil {
  123. return
  124. }
  125. h := map[string]string{
  126. "x-sls-bodyrawsize": fmt.Sprintf("%v", len(body)),
  127. "Content-Type": "application/json",
  128. "Accept-Encoding": "deflate", // TODO: support lz4
  129. }
  130. r, err := request(p, "POST", "/logstores", h, body)
  131. if err != nil {
  132. return
  133. }
  134. body, err = ioutil.ReadAll(r.Body)
  135. if err != nil {
  136. return
  137. }
  138. if r.StatusCode != http.StatusOK {
  139. errMsg := &errorMessage{}
  140. err = json.Unmarshal(body, errMsg)
  141. if err != nil {
  142. err = fmt.Errorf("failed to create logstore")
  143. dump, _ := httputil.DumpResponse(r, true)
  144. fmt.Printf("%s\n", dump)
  145. return
  146. }
  147. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  148. return
  149. }
  150. return
  151. }
  152. // DeleteLogStore deletes a logstore according by logstore name.
  153. func (p *LogProject) DeleteLogStore(name string) (err error) {
  154. h := map[string]string{
  155. "x-sls-bodyrawsize": "0",
  156. }
  157. r, err := request(p, "DELETE", "/logstores/"+name, h, nil)
  158. if err != nil {
  159. return
  160. }
  161. body, err := ioutil.ReadAll(r.Body)
  162. if err != nil {
  163. return
  164. }
  165. if r.StatusCode != http.StatusOK {
  166. errMsg := &errorMessage{}
  167. err = json.Unmarshal(body, errMsg)
  168. if err != nil {
  169. err = fmt.Errorf("failed to delete logstore")
  170. dump, _ := httputil.DumpResponse(r, true)
  171. fmt.Printf("%s\n", dump)
  172. return
  173. }
  174. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  175. return
  176. }
  177. return
  178. }
  179. // UpdateLogStore updates a logstore according by logstore name,
  180. // obviously we can't modify the logstore name itself.
  181. func (p *LogProject) UpdateLogStore(name string, ttl, shardCnt int) (err error) {
  182. type Body struct {
  183. Name string `json:"logstoreName"`
  184. TTL int `json:"ttl"`
  185. ShardCount int `json:"shardCount"`
  186. }
  187. store := &Body{
  188. Name: name,
  189. TTL: ttl,
  190. ShardCount: shardCnt,
  191. }
  192. body, err := json.Marshal(store)
  193. if err != nil {
  194. return
  195. }
  196. h := map[string]string{
  197. "x-sls-bodyrawsize": fmt.Sprintf("%v", len(body)),
  198. "Content-Type": "application/json",
  199. "Accept-Encoding": "deflate", // TODO: support lz4
  200. }
  201. r, err := request(p, "PUT", "/logstores", h, body)
  202. if err != nil {
  203. return
  204. }
  205. body, err = ioutil.ReadAll(r.Body)
  206. if err != nil {
  207. return
  208. }
  209. if r.StatusCode != http.StatusOK {
  210. errMsg := &errorMessage{}
  211. err = json.Unmarshal(body, errMsg)
  212. if err != nil {
  213. err = fmt.Errorf("failed to update logstore")
  214. dump, _ := httputil.DumpResponse(r, true)
  215. fmt.Printf("%s\n", dump)
  216. return
  217. }
  218. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  219. return
  220. }
  221. return
  222. }
  223. // ListMachineGroup returns machine group name list and the total number of machine groups.
  224. // The offset starts from 0 and the size is the max number of machine groups could be returned.
  225. func (p *LogProject) ListMachineGroup(offset, size int) (m []string, total int, err error) {
  226. h := map[string]string{
  227. "x-sls-bodyrawsize": "0",
  228. }
  229. if size <= 0 {
  230. size = 500
  231. }
  232. uri := fmt.Sprintf("/machinegroups?offset=%v&size=%v", offset, size)
  233. r, err := request(p, "GET", uri, h, nil)
  234. if err != nil {
  235. return
  236. }
  237. buf, err := ioutil.ReadAll(r.Body)
  238. if err != nil {
  239. return
  240. }
  241. if r.StatusCode != http.StatusOK {
  242. errMsg := &errorMessage{}
  243. err = json.Unmarshal(buf, errMsg)
  244. if err != nil {
  245. err = fmt.Errorf("failed to list machine group")
  246. dump, _ := httputil.DumpResponse(r, true)
  247. fmt.Printf("%s\n", dump)
  248. return
  249. }
  250. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  251. return
  252. }
  253. type Body struct {
  254. MachineGroups []string
  255. Count int
  256. Total int
  257. }
  258. body := &Body{}
  259. err = json.Unmarshal(buf, body)
  260. if err != nil {
  261. return
  262. }
  263. m = body.MachineGroups
  264. total = body.Total
  265. return
  266. }
  267. // GetMachineGroup retruns machine group according by machine group name.
  268. func (p *LogProject) GetMachineGroup(name string) (m *MachineGroup, err error) {
  269. h := map[string]string{
  270. "x-sls-bodyrawsize": "0",
  271. }
  272. r, err := request(p, "GET", "/machinegroups/"+name, h, nil)
  273. if err != nil {
  274. return
  275. }
  276. buf, err := ioutil.ReadAll(r.Body)
  277. if err != nil {
  278. return
  279. }
  280. if r.StatusCode != http.StatusOK {
  281. errMsg := &errorMessage{}
  282. err = json.Unmarshal(buf, errMsg)
  283. if err != nil {
  284. err = fmt.Errorf("failed to get machine group:%v", name)
  285. dump, _ := httputil.DumpResponse(r, true)
  286. fmt.Printf("%s\n", dump)
  287. return
  288. }
  289. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  290. return
  291. }
  292. m = &MachineGroup{}
  293. err = json.Unmarshal(buf, m)
  294. if err != nil {
  295. return
  296. }
  297. m.project = p
  298. return
  299. }
  300. // CreateMachineGroup creates a new machine group in SLS.
  301. func (p *LogProject) CreateMachineGroup(m *MachineGroup) (err error) {
  302. body, err := json.Marshal(m)
  303. if err != nil {
  304. return
  305. }
  306. h := map[string]string{
  307. "x-sls-bodyrawsize": fmt.Sprintf("%v", len(body)),
  308. "Content-Type": "application/json",
  309. "Accept-Encoding": "deflate", // TODO: support lz4
  310. }
  311. r, err := request(p, "POST", "/machinegroups", h, body)
  312. if err != nil {
  313. return
  314. }
  315. body, err = ioutil.ReadAll(r.Body)
  316. if err != nil {
  317. return
  318. }
  319. if r.StatusCode != http.StatusOK {
  320. errMsg := &errorMessage{}
  321. err = json.Unmarshal(body, errMsg)
  322. if err != nil {
  323. err = fmt.Errorf("failed to create machine group")
  324. dump, _ := httputil.DumpResponse(r, true)
  325. fmt.Printf("%s\n", dump)
  326. return
  327. }
  328. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  329. return
  330. }
  331. return
  332. }
  333. // UpdateMachineGroup updates a machine group.
  334. func (p *LogProject) UpdateMachineGroup(m *MachineGroup) (err error) {
  335. body, err := json.Marshal(m)
  336. if err != nil {
  337. return
  338. }
  339. h := map[string]string{
  340. "x-sls-bodyrawsize": fmt.Sprintf("%v", len(body)),
  341. "Content-Type": "application/json",
  342. "Accept-Encoding": "deflate", // TODO: support lz4
  343. }
  344. r, err := request(p, "PUT", "/machinegroups/"+m.Name, h, body)
  345. if err != nil {
  346. return
  347. }
  348. body, err = ioutil.ReadAll(r.Body)
  349. if err != nil {
  350. return
  351. }
  352. if r.StatusCode != http.StatusOK {
  353. errMsg := &errorMessage{}
  354. err = json.Unmarshal(body, errMsg)
  355. if err != nil {
  356. err = fmt.Errorf("failed to update machine group")
  357. dump, _ := httputil.DumpResponse(r, true)
  358. fmt.Printf("%s\n", dump)
  359. return
  360. }
  361. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  362. return
  363. }
  364. return
  365. }
  366. // DeleteMachineGroup deletes machine group according machine group name.
  367. func (p *LogProject) DeleteMachineGroup(name string) (err error) {
  368. h := map[string]string{
  369. "x-sls-bodyrawsize": "0",
  370. }
  371. r, err := request(p, "DELETE", "/machinegroups/"+name, h, nil)
  372. if err != nil {
  373. return
  374. }
  375. body, err := ioutil.ReadAll(r.Body)
  376. if err != nil {
  377. return
  378. }
  379. if r.StatusCode != http.StatusOK {
  380. errMsg := &errorMessage{}
  381. err = json.Unmarshal(body, errMsg)
  382. if err != nil {
  383. err = fmt.Errorf("failed to delete machine group")
  384. dump, _ := httputil.DumpResponse(r, true)
  385. fmt.Printf("%s\n", dump)
  386. return
  387. }
  388. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  389. return
  390. }
  391. return
  392. }
  393. // ListConfig returns config names list and the total number of configs.
  394. // The offset starts from 0 and the size is the max number of configs could be returned.
  395. func (p *LogProject) ListConfig(offset, size int) (cfgNames []string, total int, err error) {
  396. h := map[string]string{
  397. "x-sls-bodyrawsize": "0",
  398. }
  399. if size <= 0 {
  400. size = 100
  401. }
  402. uri := fmt.Sprintf("/configs?offset=%v&size=%v", offset, size)
  403. r, err := request(p, "GET", uri, h, nil)
  404. if err != nil {
  405. return
  406. }
  407. buf, err := ioutil.ReadAll(r.Body)
  408. if err != nil {
  409. return
  410. }
  411. if r.StatusCode != http.StatusOK {
  412. errMsg := &errorMessage{}
  413. err = json.Unmarshal(buf, errMsg)
  414. if err != nil {
  415. err = fmt.Errorf("failed to delete machine group")
  416. dump, _ := httputil.DumpResponse(r, true)
  417. fmt.Printf("%s\n", dump)
  418. return
  419. }
  420. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  421. return
  422. }
  423. type Body struct {
  424. Total int
  425. Configs []string
  426. }
  427. body := &Body{}
  428. err = json.Unmarshal(buf, body)
  429. if err != nil {
  430. return
  431. }
  432. cfgNames = body.Configs
  433. total = body.Total
  434. return
  435. }
  436. // GetConfig returns config according by config name.
  437. func (p *LogProject) GetConfig(name string) (c *LogConfig, err error) {
  438. h := map[string]string{
  439. "x-sls-bodyrawsize": "0",
  440. }
  441. r, err := request(p, "GET", "/configs/"+name, h, nil)
  442. if err != nil {
  443. return
  444. }
  445. buf, err := ioutil.ReadAll(r.Body)
  446. if err != nil {
  447. return
  448. }
  449. if r.StatusCode != http.StatusOK {
  450. errMsg := &errorMessage{}
  451. err = json.Unmarshal(buf, errMsg)
  452. if err != nil {
  453. err = fmt.Errorf("failed to delete config")
  454. dump, _ := httputil.DumpResponse(r, true)
  455. fmt.Printf("%s\n", dump)
  456. return
  457. }
  458. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  459. return
  460. }
  461. c = &LogConfig{}
  462. err = json.Unmarshal(buf, c)
  463. if err != nil {
  464. return
  465. }
  466. c.project = p
  467. return
  468. }
  469. // UpdateConfig updates a config.
  470. func (p *LogProject) UpdateConfig(c *LogConfig) (err error) {
  471. body, err := json.Marshal(c)
  472. if err != nil {
  473. return
  474. }
  475. h := map[string]string{
  476. "x-sls-bodyrawsize": fmt.Sprintf("%v", len(body)),
  477. "Content-Type": "application/json",
  478. "Accept-Encoding": "deflate", // TODO: support lz4
  479. }
  480. r, err := request(p, "PUT", "/configs/"+c.Name, h, body)
  481. if err != nil {
  482. return
  483. }
  484. body, err = ioutil.ReadAll(r.Body)
  485. if err != nil {
  486. return
  487. }
  488. if r.StatusCode != http.StatusOK {
  489. errMsg := &errorMessage{}
  490. err = json.Unmarshal(body, errMsg)
  491. if err != nil {
  492. err = fmt.Errorf("failed to update config")
  493. dump, _ := httputil.DumpResponse(r, true)
  494. fmt.Printf("%s\n", dump)
  495. return
  496. }
  497. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  498. return
  499. }
  500. return
  501. }
  502. // CreateConfig creates a new config in SLS.
  503. func (p *LogProject) CreateConfig(c *LogConfig) (err error) {
  504. body, err := json.Marshal(c)
  505. if err != nil {
  506. return
  507. }
  508. h := map[string]string{
  509. "x-sls-bodyrawsize": fmt.Sprintf("%v", len(body)),
  510. "Content-Type": "application/json",
  511. "Accept-Encoding": "deflate", // TODO: support lz4
  512. }
  513. r, err := request(p, "POST", "/configs", h, body)
  514. if err != nil {
  515. return
  516. }
  517. body, err = ioutil.ReadAll(r.Body)
  518. if err != nil {
  519. return
  520. }
  521. if r.StatusCode != http.StatusOK {
  522. errMsg := &errorMessage{}
  523. err = json.Unmarshal(body, errMsg)
  524. if err != nil {
  525. err = fmt.Errorf("failed to update config")
  526. dump, _ := httputil.DumpResponse(r, true)
  527. fmt.Printf("%s\n", dump)
  528. return
  529. }
  530. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  531. return
  532. }
  533. return
  534. }
  535. // DeleteConfig deletes a config according by config name.
  536. func (p *LogProject) DeleteConfig(name string) (err error) {
  537. h := map[string]string{
  538. "x-sls-bodyrawsize": "0",
  539. }
  540. r, err := request(p, "DELETE", "/configs/"+name, h, nil)
  541. if err != nil {
  542. return
  543. }
  544. body, err := ioutil.ReadAll(r.Body)
  545. if err != nil {
  546. return
  547. }
  548. if r.StatusCode != http.StatusOK {
  549. errMsg := &errorMessage{}
  550. err = json.Unmarshal(body, errMsg)
  551. if err != nil {
  552. err = fmt.Errorf("failed to delete config")
  553. dump, _ := httputil.DumpResponse(r, true)
  554. fmt.Printf("%s\n", dump)
  555. return
  556. }
  557. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  558. return
  559. }
  560. return
  561. }
  562. // GetAppliedMachineGroups returns applied machine group names list according config name.
  563. func (p *LogProject) GetAppliedMachineGroups(confName string) (groupNames []string, err error) {
  564. h := map[string]string{
  565. "x-sls-bodyrawsize": "0",
  566. }
  567. uri := fmt.Sprintf("/configs/%v/machinegroups", confName)
  568. r, err := request(p, "GET", uri, h, nil)
  569. if err != nil {
  570. return
  571. }
  572. buf, err := ioutil.ReadAll(r.Body)
  573. if err != nil {
  574. return
  575. }
  576. if r.StatusCode != http.StatusOK {
  577. errMsg := &errorMessage{}
  578. err = json.Unmarshal(buf, errMsg)
  579. if err != nil {
  580. err = fmt.Errorf("failed to get applied machine groups")
  581. dump, _ := httputil.DumpResponse(r, true)
  582. fmt.Printf("%s\n", dump)
  583. return
  584. }
  585. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  586. return
  587. }
  588. type Body struct {
  589. Count int
  590. Machinegroups []string
  591. }
  592. body := &Body{}
  593. err = json.Unmarshal(buf, body)
  594. if err != nil {
  595. return
  596. }
  597. groupNames = body.Machinegroups
  598. return
  599. }
  600. // GetAppliedConfigs returns applied config names list according machine group name groupName.
  601. func (p *LogProject) GetAppliedConfigs(groupName string) (confNames []string, err error) {
  602. h := map[string]string{
  603. "x-sls-bodyrawsize": "0",
  604. }
  605. uri := fmt.Sprintf("/machinegroups/%v/configs", groupName)
  606. r, err := request(p, "GET", uri, h, nil)
  607. if err != nil {
  608. return
  609. }
  610. buf, err := ioutil.ReadAll(r.Body)
  611. if err != nil {
  612. return
  613. }
  614. if r.StatusCode != http.StatusOK {
  615. errMsg := &errorMessage{}
  616. err = json.Unmarshal(buf, errMsg)
  617. if err != nil {
  618. err = fmt.Errorf("failed to applied configs")
  619. dump, _ := httputil.DumpResponse(r, true)
  620. fmt.Printf("%s\n", dump)
  621. return
  622. }
  623. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  624. return
  625. }
  626. type Cfg struct {
  627. Count int `json:"count"`
  628. Configs []string `json:"configs"`
  629. }
  630. body := &Cfg{}
  631. err = json.Unmarshal(buf, body)
  632. if err != nil {
  633. return
  634. }
  635. confNames = body.Configs
  636. return
  637. }
  638. // ApplyConfigToMachineGroup applies config to machine group.
  639. func (p *LogProject) ApplyConfigToMachineGroup(confName, groupName string) (err error) {
  640. h := map[string]string{
  641. "x-sls-bodyrawsize": "0",
  642. }
  643. uri := fmt.Sprintf("/machinegroups/%v/configs/%v", groupName, confName)
  644. r, err := request(p, "PUT", uri, h, nil)
  645. if err != nil {
  646. return
  647. }
  648. buf, err := ioutil.ReadAll(r.Body)
  649. if err != nil {
  650. return
  651. }
  652. if r.StatusCode != http.StatusOK {
  653. errMsg := &errorMessage{}
  654. err = json.Unmarshal(buf, errMsg)
  655. if err != nil {
  656. err = fmt.Errorf("failed to apply config to machine group")
  657. dump, _ := httputil.DumpResponse(r, true)
  658. fmt.Printf("%s\n", dump)
  659. return
  660. }
  661. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  662. return
  663. }
  664. return
  665. }
  666. // RemoveConfigFromMachineGroup removes config from machine group.
  667. func (p *LogProject) RemoveConfigFromMachineGroup(confName, groupName string) (err error) {
  668. h := map[string]string{
  669. "x-sls-bodyrawsize": "0",
  670. }
  671. uri := fmt.Sprintf("/machinegroups/%v/configs/%v", groupName, confName)
  672. r, err := request(p, "DELETE", uri, h, nil)
  673. if err != nil {
  674. return
  675. }
  676. buf, err := ioutil.ReadAll(r.Body)
  677. if err != nil {
  678. return
  679. }
  680. if r.StatusCode != http.StatusOK {
  681. errMsg := &errorMessage{}
  682. err = json.Unmarshal(buf, errMsg)
  683. if err != nil {
  684. err = fmt.Errorf("failed to remove config from machine group")
  685. dump, _ := httputil.DumpResponse(r, true)
  686. fmt.Printf("%s\n", dump)
  687. return
  688. }
  689. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  690. return
  691. }
  692. return
  693. }