Store keys as ByteStrings
[ganeti-github.git] / src / Ganeti / WConfd / ConfigModifications.hs
1 {-# LANGUAGE TemplateHaskell #-}
2
3 {-| The WConfd functions for direct configuration manipulation
4
5 This module contains the client functions exported by WConfD for
6 specific configuration manipulation.
7
8 -}
9
10 {-
11
12 Copyright (C) 2014 Google Inc.
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are
17 met:
18
19 1. Redistributions of source code must retain the above copyright notice,
20 this list of conditions and the following disclaimer.
21
22 2. Redistributions in binary form must reproduce the above copyright
23 notice, this list of conditions and the following disclaimer in the
24 documentation and/or other materials provided with the distribution.
25
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
30 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
38 -}
39
40 module Ganeti.WConfd.ConfigModifications where
41
42 import qualified Data.ByteString.UTF8 as UTF8
43 import Control.Lens.Setter ((.~))
44 import Control.Lens.Traversal (mapMOf)
45 import Data.Maybe (isJust)
46 import Language.Haskell.TH (Name)
47
48 import Ganeti.JSON (alterContainerL)
49 import Ganeti.Locking.Locks (ClientId, ciIdentifier)
50 import Ganeti.Logging.Lifted (logDebug)
51 import Ganeti.Objects
52 import Ganeti.Objects.Lens
53 import Ganeti.WConfd.ConfigState (csConfigDataL)
54 import Ganeti.WConfd.Monad (WConfdMonad, modifyConfigWithLock)
55 import qualified Ganeti.WConfd.TempRes as T
56
57
58 -- | Add a new instance to the configuration, release DRBD minors,
59 -- and commit temporary IPs, all while temporarily holding the config
60 -- lock. Return True upon success and False if the config lock was not
61 -- available and the client should retry.
62 --
63 -- TODO: add verifications to this call; the client should have a lock
64 -- on the name of the instance, and no instance with the given uuid should
65 -- exist.
66 addInstance :: Instance -> ClientId -> WConfdMonad Bool
67 addInstance inst cid = do
68 logDebug $ "AddInstance: client " ++ show (ciIdentifier cid)
69 ++ " adding instance " ++ uuidOf inst
70 ++ " with name " ++ show (instName inst)
71 let addInst = csConfigDataL . configInstancesL
72 . alterContainerL (UTF8.fromString $ uuidOf inst)
73 .~ Just inst
74 commitRes tr = mapMOf csConfigDataL $ T.commitReservedIps cid tr
75 r <- modifyConfigWithLock
76 (\tr cs -> commitRes tr $ addInst cs)
77 . T.releaseDRBDMinors . UTF8.fromString $ uuidOf inst
78 logDebug $ "AddInstance: result of config modification is " ++ show r
79 return $ isJust r
80
81 -- * The list of functions exported to RPC.
82
83 exportedFunctions :: [Name]
84 exportedFunctions = [ 'addInstance
85 ]