(ctx context.Context, instanceID string, apis map[string]*runtimev1.API)
| 179 | } |
| 180 | |
| 181 | func (s *Server) generateOpenAPISpec(ctx context.Context, instanceID string, apis map[string]*runtimev1.API) (*openapi3.T, error) { |
| 182 | attributes := s.runtime.GetInstanceAttributes(ctx, instanceID) |
| 183 | var organization, project string |
| 184 | for _, attr := range attributes { |
| 185 | if attr.Key == "organization" { |
| 186 | organization = attr.Value.AsString() |
| 187 | } else if attr.Key == "project" { |
| 188 | project = attr.Value.AsString() |
| 189 | } |
| 190 | } |
| 191 | var title string |
| 192 | if organization != "" && project != "" { |
| 193 | title = fmt.Sprintf("Rill %s/%s project API", organization, project) |
| 194 | } else { |
| 195 | title = "Rill project API" |
| 196 | } |
| 197 | |
| 198 | spec := &openapi3.T{ |
| 199 | OpenAPI: "3.0.3", |
| 200 | Info: &openapi3.Info{ |
| 201 | Title: title, |
| 202 | Version: "1.0.0", |
| 203 | }, |
| 204 | Paths: &openapi3.Paths{}, |
| 205 | } |
| 206 | |
| 207 | var runtimeHost string |
| 208 | if s.opts.AuthAudienceURL != "" { |
| 209 | runtimeURL, err := url.Parse(s.opts.AuthAudienceURL) |
| 210 | if err != nil { |
| 211 | return nil, err |
| 212 | } |
| 213 | runtimeHost = runtimeURL.Host |
| 214 | } else { |
| 215 | runtimeHost = fmt.Sprintf("localhost:%d", s.opts.HTTPPort) |
| 216 | } |
| 217 | |
| 218 | spec.Servers = openapi3.Servers{ |
| 219 | &openapi3.Server{ |
| 220 | URL: fmt.Sprintf("http://%s/v1/instances/%s/api", runtimeHost, instanceID), |
| 221 | }, |
| 222 | } |
| 223 | |
| 224 | for name, api := range apis { |
| 225 | pathItem, componentsForPath, err := s.generatePathItemSpec(name, api) |
| 226 | if err != nil { |
| 227 | return nil, err |
| 228 | } |
| 229 | |
| 230 | spec.Paths.Set(fmt.Sprintf("/%s", name), pathItem) |
| 231 | |
| 232 | for k, v := range componentsForPath { |
| 233 | if spec.Components == nil { |
| 234 | spec.Components = &openapi3.Components{} |
| 235 | } |
| 236 | if spec.Components.Schemas == nil { |
| 237 | spec.Components.Schemas = make(map[string]*openapi3.SchemaRef) |
| 238 | } |
no test coverage detected