d4e6240f0f0bebb9bff5cec37d1c8706a23615c1 — Louis Solofrizzo 6 months ago 30a3187
api: Fix image naming by hiding the architecture from the client

Signed-off-by: Louis Solofrizzo <lsolofrizzo@online.net>
3 files changed, 33 insertions(+), 11 deletions(-)

M api/images.go
M api/instance.go
M api/main.go
M api/images.go => api/images.go +12 -6
@@ 4,6 4,7 @@ "cisco/sdk"
  	"errors"
  	"github.com/kataras/iris"
+ 	"strings"
  )
  
  func OSIsValid(ctx iris.Context, name string) error {


@@ 37,12 38,17 @@   	for _, img := range images {
  		if img.Public {
- 			result = append(result, cisco.Image{
- 				Name:         img.Aliases[0].Name,
- 				Architecture: img.Architecture,
- 				Fingerprint:  img.Fingerprint,
- 				Size:         img.Size,
- 			})
+ 			if len(img.Aliases) > 0 {
+ 				array := strings.Split(img.Aliases[0].Name, "/")
+ 				array = array[:len(array)-1]
+ 
+ 				result = append(result, cisco.Image{
+ 					Name:         strings.Join(array, "/"),
+ 					Architecture: img.Architecture,
+ 					Fingerprint:  img.Fingerprint,
+ 					Size:         img.Size,
+ 				})
+ 			}
  		}
  	}
  

M api/instance.go => api/instance.go +17 -5
@@ 121,19 121,19 @@ return errors.New("The specified Architecture is not valid")
  }
  
- func TypeIsValid(ctx iris.Context, name string) error {
+ func TypeIsValid(ctx iris.Context, name string) (string, error) {
  	var instanceTypes []DbInstanceType
  
  	DB.Find(&instanceTypes, "enabled = ?", true)
  
  	for _, instanceType := range instanceTypes {
  		if instanceType.Name == name {
- 			return nil
+ 			return instanceType.Architecture, nil
  		}
  	}
  
  	APIError(ctx, 400, "The specified Instance Type is not valid")
- 	return errors.New("The specified Instance Type is not valid")
+ 	return "", errors.New("The specified Instance Type is not valid")
  }
  
  func createBackendInstance(instance DbInstance) error {


@@ 201,11 201,18 @@ return
  	}
  
- 	if OSIsValid(ctx, instance.OS) != nil {
+ 	arch, err := TypeIsValid(ctx, instance.Type)
+ 	if err != nil {
  		return
  	}
  
- 	if TypeIsValid(ctx, instance.Type) != nil {
+ 	if arch == "x86_64" {
+ 		instance.OS += "/intel"
+ 	} else if arch == "arm" {
+ 		instance.OS += "/arm"
+ 	}
+ 
+ 	if OSIsValid(ctx, instance.OS) != nil {
  		return
  	}
  


@@ 235,6 242,9 @@ return
  	}
  
+ 	array := strings.Split(instance.OS, "/")
+ 	array = array[:len(array)-1]
+ 
  	entry.Uuid = uuid.NewV4().String()
  	entry.Name = instance.Name
  	entry.Type = instance.Type


@@ 249,7 259,9 @@ return
  	}
  
+ 	instance.OS = strings.Join(array, "/")
  	DB.Model(&entry).Update("status", "LXD")
+ 	DB.Model(&entry).Update("os", instance.OS)
  	ctx.StatusCode(iris.StatusCreated)
  }
  

M api/main.go => api/main.go +4 -0
@@ 153,6 153,10 @@ }
  	})
  
+ 	app.Get("/", func(ctx iris.Context) {
+ 		ctx.Writef("cisco-api: %s-%s (%s)\n", cisco.GitTag(), cisco.GitRev(), cisco.GitBranch())
+ 	})
+ 
  	InitInviteRoutes(app)
  	InitTokenRoutes(app)
  	InitAdminRoutes(app)