[lxc-devel] [lxd/master] Re-create the project default profile when turning on the project profiles feature

freeekanayaka on Github lxc-bot at linuxcontainers.org
Fri Oct 12 13:29:07 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 377 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20181012/5627c65c/attachment.bin>
-------------- next part --------------
From 4ded3eb9702236d1925c5e10bb35f1fc2eaeb24b Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanayaka at canonical.com>
Date: Fri, 12 Oct 2018 15:27:05 +0200
Subject: [PATCH] Re-create the project default profile when turning on the
 project profiles feature

Signed-off-by: Free Ekanayaka <free.ekanayaka at canonical.com>
---
 lxd/api_project.go      | 46 +++++++++++++++++++++++++++--------------
 test/suites/projects.sh | 14 +++++++++++++
 2 files changed, 45 insertions(+), 15 deletions(-)

diff --git a/lxd/api_project.go b/lxd/api_project.go
index 5f350ade07..b4f7c25259 100644
--- a/lxd/api_project.go
+++ b/lxd/api_project.go
@@ -95,17 +95,9 @@ func apiProjectsPost(d *Daemon, r *http.Request) Response {
 		}
 
 		if project.Config["features.profiles"] == "true" {
-			// Create a default profile
-			profile := db.Profile{}
-			profile.Project = project.Name
-			profile.Name = "default"
-			profile.Description = fmt.Sprintf("Default LXD profile for project %s", project.Name)
-			profile.Config = map[string]string{}
-			profile.Devices = types.Devices{}
-
-			_, err = tx.ProfileCreate(profile)
+			err = apiProjectCreateDefaultProfile(tx, project.Name)
 			if err != nil {
-				return errors.Wrap(err, "Add default profile to database")
+				return err
 			}
 		}
 
@@ -118,6 +110,23 @@ func apiProjectsPost(d *Daemon, r *http.Request) Response {
 	return SyncResponseLocation(true, nil, fmt.Sprintf("/%s/projects/%s", version.APIVersion, project.Name))
 }
 
+// Create the default profile of a project.
+func apiProjectCreateDefaultProfile(tx *db.ClusterTx, project string) error {
+	// Create a default profile
+	profile := db.Profile{}
+	profile.Project = project
+	profile.Name = "default"
+	profile.Description = fmt.Sprintf("Default LXD profile for project %s", project)
+	profile.Config = map[string]string{}
+	profile.Devices = types.Devices{}
+
+	_, err := tx.ProfileCreate(profile)
+	if err != nil {
+		return errors.Wrap(err, "Add default profile to database")
+	}
+	return nil
+}
+
 func apiProjectGet(d *Daemon, r *http.Request) Response {
 	name := mux.Vars(r)["name"]
 
@@ -260,11 +269,18 @@ func apiProjectChange(d *Daemon, project *api.Project, req api.ProjectPut) Respo
 			return errors.Wrap(err, "Persist profile changes")
 		}
 
-		if req.Config["features.profiles"] != project.Config["features.profiles"] && req.Config["features.profiles"] != "true" {
-			// Delete the project-specific default profile.
-			err = tx.ProfileDelete(project.Name, "default")
-			if err != nil {
-				return errors.Wrap(err, "Delete project default profile")
+		if req.Config["features.profiles"] != project.Config["features.profiles"] {
+			if req.Config["features.profiles"] == "true" {
+				err = apiProjectCreateDefaultProfile(tx, project.Name)
+				if err != nil {
+					return err
+				}
+			} else {
+				// Delete the project-specific default profile.
+				err = tx.ProfileDelete(project.Name, "default")
+				if err != nil {
+					return errors.Wrap(err, "Delete project default profile")
+				}
 			}
 		}
 
diff --git a/test/suites/projects.sh b/test/suites/projects.sh
index 5df27872f9..a8064c8146 100644
--- a/test/suites/projects.sh
+++ b/test/suites/projects.sh
@@ -36,6 +36,20 @@ test_projects_crud() {
   # Trying to rename a project using an existing name fails
   ! lxc project rename bar foo
 
+  lxc project switch foo
+
+  # Turning off the profiles feature makes the project see the default profile
+  # from the default project.
+  lxc project set foo features.profiles false
+  lxc profile show default | grep -E -q '^description: Default LXD profile$'
+
+  # Turning on the profiles feature creates a project-specific default
+  # profile.
+  lxc project set foo features.profiles true
+  lxc profile show default | grep -E -q '^description: Default LXD profile for project foo$'
+
+  lxc project switch default
+
   # Delete the projects
   lxc project delete foo
   lxc project delete bar


More information about the lxc-devel mailing list