Added seed code for caas-helm.
[ta/caas-helm.git] / src / chart-repo-handler / pkg / api / handlers.go
1 // Copyright 2019 Nokia
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package api
16
17 import (
18         "fmt"
19         "io"
20         "log"
21         "net/http"
22         "strings"
23
24         "pkg/config"
25         "pkg/repo"
26
27         "github.com/gorilla/mux"
28         "github.com/ncw/swift"
29 )
30
31 func Index(c *swift.Connection, config config.EnvConfig) http.HandlerFunc {
32         return func(w http.ResponseWriter, r *http.Request) {
33                 fmt.Fprintln(w, "<html><body>")
34                 fmt.Fprintln(w, "Hello World!<br />")
35                 objects, err := c.ObjectNames(config.Container, nil)
36                 if err != nil {
37                         log.Println("Index error:", err)
38                         fmt.Fprintln(w, "Index error:", err)
39                 } else {
40                         for _, v := range objects {
41                                 fmt.Fprintf(w, "<a href=\"%s\">%s</a><br />", v, v)
42                         }
43                 }
44                 fmt.Fprintln(w, "</body></html>")
45         }
46 }
47
48 func ObjectGet(c *swift.Connection, config config.EnvConfig) http.HandlerFunc {
49         return func(w http.ResponseWriter, r *http.Request) {
50                 vars := mux.Vars(r)
51                 objectName := vars["id"]
52
53                 object, _, err := c.ObjectOpen(config.Container, objectName, false, nil)
54
55                 if err != nil {
56                         switch err.Error() {
57                         case "Object Not Found":
58                                 w.WriteHeader(http.StatusNotFound)
59                         default:
60                                 w.WriteHeader(http.StatusInternalServerError)
61                         }
62                         log.Println("ObjectGet error:", err, ", requested object:", objectName)
63                         fmt.Fprintln(w, "<html><body>")
64                         fmt.Fprintln(w, "ObjectGet error:", err)
65                         fmt.Fprintln(w, "</body></html>")
66                         return
67                 }
68                 io.Copy(w, object)
69                 object.Close()
70         }
71 }
72
73 func ObjectPut(c *swift.Connection, config config.EnvConfig) http.HandlerFunc {
74         return func(w http.ResponseWriter, r *http.Request) {
75                 vars := mux.Vars(r)
76                 objectName := vars["id"]
77
78                 _, err := c.ObjectPut(config.Container, objectName, r.Body, false, "", "", nil)
79                 if err != nil {
80                         log.Println("ObjectPut error:", err.Error())
81                         w.WriteHeader(http.StatusInternalServerError)
82                         fmt.Fprintln(w, "<html><body>")
83                         fmt.Fprintln(w, "ObjectPut error:", err)
84                         fmt.Fprintln(w, "</body></html>")
85                         return
86                 }
87                 if strings.HasPrefix(objectName, config.IndexPath) && strings.HasSuffix(objectName, "tgz") {
88                         log.Println("Regenerating index for:", config.IndexPath)
89                         if err := repo.Index(c, config.Container, config.RepoUrl+":"+config.ListenOnPort, config.IndexPath, "/index.yaml"); err != nil {
90                                 log.Println("ObjectPut error:", err.Error())
91                                 fmt.Fprintln(w, "<html><body>")
92                                 fmt.Fprintln(w, "Index file generation error:", err)
93                                 fmt.Fprintln(w, "</body></html>")
94                         }
95                 }
96         }
97 }
98
99 func ObjectDelete(c *swift.Connection, config config.EnvConfig) http.HandlerFunc {
100         return func(w http.ResponseWriter, r *http.Request) {
101                 vars := mux.Vars(r)
102                 objectName := vars["id"]
103
104                 err := c.ObjectDelete(config.Container, objectName)
105                 if err != nil {
106                         log.Println("ObjectDelete error:", err.Error())
107                         fmt.Fprintln(w, "<html><body>")
108                         fmt.Fprintln(w, "ObjectDelete error:", err)
109                         fmt.Fprintln(w, "</body></html>")
110                         return
111                 }
112                 if strings.HasPrefix(objectName, config.IndexPath) && strings.HasSuffix(objectName, "tgz") {
113                         log.Println("Regenerating index for:", config.IndexPath)
114                         if err := repo.Index(c, config.Container, config.RepoUrl+":"+config.ListenOnPort, config.IndexPath, "/index.yaml"); err != nil {
115                                 log.Println("ObjectPut error:", err.Error())
116                                 fmt.Fprintln(w, "<html><body>")
117                                 fmt.Fprintln(w, "Index file generation error:", err)
118                                 fmt.Fprintln(w, "</body></html>")
119                         }
120                 }
121         }
122 }