Ejemplo n.º 1
0
func (s *SwaggerAST) ConvertIntoApiVersion(asMock bool) (tykcommon.VersionInfo, error) {
	thisVersionInfo := tykcommon.VersionInfo{}

	if asMock {
		return thisVersionInfo, errors.New("Swagger mocks not supported")
	}

	thisVersionInfo.UseExtendedPaths = true
	thisVersionInfo.Name = s.Info.Version
	thisVersionInfo.ExtendedPaths.WhiteList = make([]tykcommon.EndPointMeta, 0)

	if len(s.Paths) == 0 {
		return thisVersionInfo, errors.New("no paths defined in swagger file")
	}
	for pathName, pathSpec := range s.Paths {
		log.Debug("path: %s", pathName)
		newEndpointMeta := tykcommon.EndPointMeta{}
		newEndpointMeta.MethodActions = make(map[string]tykcommon.EndpointMethodMeta)
		newEndpointMeta.Path = pathName

		// We just want the paths here, no mocks
		methods := map[string]PathMethodObject{
			"GET":     pathSpec.Get,
			"PUT":     pathSpec.Put,
			"POST":    pathSpec.Post,
			"HEAD":    pathSpec.Head,
			"PATCH":   pathSpec.Patch,
			"OPTIONS": pathSpec.Options,
			"DELETE":  pathSpec.Delete,
		}
		for methodName, m := range methods {
			// skip methods that are not defined
			if len(m.Responses) == 0 && m.Description == "" && m.OperationID == "" {
				continue
			}
			thisMethodAction := tykcommon.EndpointMethodMeta{}
			thisMethodAction.Action = tykcommon.NoAction
			newEndpointMeta.MethodActions[methodName] = thisMethodAction
		}

		thisVersionInfo.ExtendedPaths.WhiteList = append(thisVersionInfo.ExtendedPaths.WhiteList, newEndpointMeta)
	}

	return thisVersionInfo, nil
}
Ejemplo n.º 2
0
func (b *BluePrintAST) ConvertIntoApiVersion(asMock bool) (tykcommon.VersionInfo, error) {
	thisVersionInfo := tykcommon.VersionInfo{}
	thisVersionInfo.UseExtendedPaths = true
	thisVersionInfo.Name = b.Name

	if len(b.ResourceGroups) < 1 {
		return thisVersionInfo, errors.New("There are no resource groups defined in this blueprint, are you sure it is correctly formatted?")
	}

	for _, resourceGroup := range b.ResourceGroups {
		if len(resourceGroup.Resources) < 1 {
			return thisVersionInfo, errors.New("no resourcs defined in the resource group")
		}

		for _, resource := range resourceGroup.Resources {
			newMetaData := tykcommon.EndPointMeta{}
			newMetaData.Path = resource.UriTemplate
			newMetaData.MethodActions = make(map[string]tykcommon.EndpointMethodMeta)

			for _, action := range resource.Actions {
				if len(action.Examples) > 0 {
					if len(action.Examples[0].Responses) > 0 {
						thisEndPointMethodMeta := tykcommon.EndpointMethodMeta{}
						code, err := strconv.Atoi(action.Examples[0].Responses[0].Name)
						if err != nil {
							log.Warning("Could not genrate response code form Name field, using 200")
							code = 200
						}
						thisEndPointMethodMeta.Code = code

						if asMock {
							thisEndPointMethodMeta.Action = tykcommon.Reply
						} else {
							thisEndPointMethodMeta.Action = tykcommon.NoAction
						}

						for _, h := range action.Examples[0].Responses[0].Headers {
							thisEndPointMethodMeta.Headers = make(map[string]string)
							thisEndPointMethodMeta.Headers[h.Name] = h.Value
						}
						thisEndPointMethodMeta.Data = action.Examples[0].Responses[0].Body
						newMetaData.MethodActions[action.Method] = thisEndPointMethodMeta
					}
				}
			}

			// Add it to the version
			thisVersionInfo.ExtendedPaths.WhiteList = make([]tykcommon.EndPointMeta, 0)
			thisVersionInfo.ExtendedPaths.WhiteList = append(thisVersionInfo.ExtendedPaths.WhiteList, newMetaData)
		}

	}

	return thisVersionInfo, nil
}