From e98d7e6890dc9443b9885b1a3ee69a64ccaafa94 Mon Sep 17 00:00:00 2001
From: andrewbogott <abogott@wikimedia.org>
Date: Wed, 16 May 2018 23:08:41 -0500
Subject: [PATCH] Image panel: check instance create policy for 'Launch' button

Closes-Bug: #1771851
Change-Id: If77746cbbe73540218b90c516e5f8802c002854f
---
 .../core/images/actions/launch-instance.service.js    |  8 +++++++-
 .../images/actions/launch-instance.service.spec.js    | 11 +++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/openstack_dashboard/static/app/core/images/actions/launch-instance.service.js b/openstack_dashboard/static/app/core/images/actions/launch-instance.service.js
index e54fd072d1..5a6ced5fdd 100644
--- a/openstack_dashboard/static/app/core/images/actions/launch-instance.service.js
+++ b/openstack_dashboard/static/app/core/images/actions/launch-instance.service.js
@@ -23,6 +23,7 @@
   launchInstanceService.$inject = [
     '$q',
     'horizon.app.core.images.non_bootable_image_types',
+    'horizon.app.core.openstack-service-api.policy',
     'horizon.dashboard.project.workflow.launch-instance.modal.service',
     'horizon.framework.util.q.extensions'
   ];
@@ -44,6 +45,7 @@
   function launchInstanceService(
     $q,
     nonBootableImageTypes,
+    policy,
     launchInstanceModal,
     $qExtensions
   ) {
@@ -66,7 +68,11 @@
     }
 
     function allowed(image) {
-      return $q.all([isBootable(image), isActive(image)]);
+      return $q.all([
+        isBootable(image),
+        isActive(image),
+        policy.ifAllowed({ rules: [['compute', 'compute:create']] })
+      ]);
     }
 
     function isActive(image) {
diff --git a/openstack_dashboard/static/app/core/images/actions/launch-instance.service.spec.js b/openstack_dashboard/static/app/core/images/actions/launch-instance.service.spec.js
index 91f8dbab71..fa40ed54c1 100644
--- a/openstack_dashboard/static/app/core/images/actions/launch-instance.service.spec.js
+++ b/openstack_dashboard/static/app/core/images/actions/launch-instance.service.spec.js
@@ -22,6 +22,16 @@
       open: function () {}
     };
 
+    var policyAPI = {
+      ifAllowed: function() {
+        return {
+          success: function(callback) {
+            callback({allowed: true});
+          }
+        };
+      }
+    };
+
     var service, $scope;
 
     ///////////////////////
@@ -31,6 +41,7 @@
       $provide.value(
         'horizon.dashboard.project.workflow.launch-instance.modal.service', launchInstanceModalMock
       );
+      $provide.value('horizon.app.core.openstack-service-api.policy', policyAPI);
     }));
 
     beforeEach(inject(function($injector, _$rootScope_) {